定制 typicms/bootforms 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

typicms/bootforms

Composer 安装命令:

composer require typicms/bootforms

包简介

Just a Formbuilder with some Bootstrap 5 specific conveniences. Remembers old input, retrieves error messages and handles all your boilerplate Bootstrap markup automatically.

README 文档

README

Latest Version on Packagist tests Rector Pint

BootForms was originally created by Adam Wathan. It is build on top of the more general Form package by adding another layer of abstraction to rapidly generate markup for standard Bootstrap 5 forms. Probably not perfect for your super custom branded ready-for-release apps, but a huge time saver when you are still in the prototyping stage!

Installing with Composer

You can install this package via Composer by running this command in your terminal in the root of your project:

composer require typicms/bootforms

Laravel

If you are using Laravel 4 or 5, you can get started very quickly by registering the included service provider.

Modify the providers array in config/app.php to include the BootFormsServiceProvider:

'providers' => [
    //...
    'TypiCMS\BootForms\BootFormsServiceProvider'
  ],

Add the BootForm facade to the aliases array in config/app.php:

'aliases' => [
    //...
    'BootForm' => 'TypiCMS\BootForms\Facades\BootForm'
  ],

You can now start using BootForms by calling methods directly on the BootForm facade:

BootForm::text('Email', 'email');

Outside of Laravel

Usage outside of Laravel is a little trickier since there’s a bit of a dependency stack you need to build up, but it’s not too tricky.

$formBuilder = new TypiCMS\Form\FormBuilder;

$formBuilder->setOldInputProvider($myOldInputProvider);
$formBuilder->setErrorStore($myErrorStore);
$formBuilder->setToken($myCsrfToken);

$basicBootFormsBuilder = new TypiCMS\BootForms\BasicFormBuilder($formBuilder);
$horizontalBootFormsBuilder = new TypiCMS\BootForms\HorizontalFormBuilder($formBuilder);

$bootForm = new TypiCMS\BootForms\BootForm($basicBootFormsBuilder, $horizontalBootFormsBuilder);

Note: You must provide your own implementations of TypiCMS\Form\OldInputInterface and TypiCMS\Form\ErrorStoreInterface when not using the implementations meant for Laravel.

Using BootForms

Basic Usage

BootForms lets you create a label and form control and wrap it all in a form group in one call.

//  <form method="post">
//    <div class="mb-3">
//      <label for="field_name" class="form-label">Field Label</label>
//      <input type="text" class="form-control" id="field_name" name="field_name">
//    </div>
//  </form>
{!! BootForm::open() !!}
{!! BootForm::text('Field Label', 'field_name') !!}
{!! BootForm::close() !!}

Note: Don’t forget to open() forms before trying to create fields! BootForms needs to know if you opened a vertical or horizontal form before it can render a field, so you’ll get an error if you forget.

Customizing Elements

If you need to customize your form elements in any way (such as adding a default value or placeholder to a text element), simply chain the calls you need to make and they will fall through to the underlying form element.

Attributes can be added either via the attribute method, or by simply using the attribute name as the method name.

// <div class="mb-3">
//    <label for="first_name" class="form-label">First Name</label>
//    <input type="text" class="form-control" id="first_name" name="first_name" placeholder="John Doe">
// </div>
BootForm::text('First Name', 'first_name')->placeholder('John Doe');

// <div class="mb-3">
//   <label for="color" class="form-label">Color</label>
//   <select class="form-select" id="color" name="color">
//     <option value="red">Red</option>
//     <option value="green" selected>Green</option>
//   </select>
// </div>
BootForm::select('Color', 'color')->options(['red' => 'Red', 'green' => 'Green'])->select('green');

// <form method="GET" action="/users">
BootForm::open()->get()->action('/users');

// <div class="mb-3">
//    <label for="first_name" class="form-label">First Name</label>
//    <input type="text" class="form-control" id="first_name" name="first_name" value="John Doe">
// </div>
BootForm::text('First Name', 'first_name')->defaultValue('John Doe');

For more information about what’s possible, check out the documentation for the Form package.

Reduced Boilerplate

Typical Bootstrap form boilerplate might look something like this:

<form>
  <div class="mb-3">
    <label for="first_name" class="form-label">First Name</label>
    <input type="text" class="form-control" name="first_name" id="first_name" />
  </div>
  <div class="mb-3">
    <label for="last_name" class="form-label">Last Name</label>
    <input type="text" class="form-control" name="last_name" id="last_name" />
  </div>
  <div class="mb-3">
    <label for="date_of_birth" class="form-label">Date of Birth</label>
    <input type="date" class="form-control" name="date_of_birth" id="date_of_birth" />
  </div>
  <div class="mb-3">
    <label for="email" class="form-label">Email address</label>
    <input type="email" class="form-control" name="email" id="email" />
  </div>
  <div class="mb-3">
    <label for="password" class="form-label">Password</label>
    <input type="password" class="form-control" name="password" id="password" />
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
</form>

BootForms makes a few decisions for you and allows you to pare it down a bit more:

{!! BootForm::open() !!}
  {!! BootForm::text('First Name', 'first_name') !!}
  {!! BootForm::text('Last Name', 'last_name') !!}
  {!! BootForm::date('Date of Birth', 'date_of_birth') !!}
  {!! BootForm::email('Email', 'email') !!}
  {!! BootForm::password('Password', 'password') !!}
  {!! BootForm::submit('Submit') !!}
{!! BootForm::close() !!}

Input groups

Bootforms allows you to create input groups.

//  <div class="mb-3">
//      <label for="amount" class="form-label">Amount</label>
//      <div class="input-group">
//          <span class="input-group-text">$</span>
//          <input type="number" name="amount" id="amount" class="form-control">
//      </div>
//  </div>
{!! BootForm::inputGroup('Amount', 'amount')->type('number')->beforeAddon('<span class="input-group-text">$</span>') !!}

With a button appended.

//  <div class="mb-3">
//      <label for="email">Email</label>
//      <div class="input-group">
//          <input type="text" name="email" id="email" class="form-control">
//          <button type="submit" class="btn btn-primary">OK</button>
//      </div>
//  </div>
{!! BootForm::inputGroup('Email', 'email')->afterAddon(BootForm::submit('OK')) !!}

Automatic Validation State

Another nice thing about BootForms is that it will automatically add error states and error messages to your controls if it sees an error for that control in the error store.

Essentially, this takes code that would normally look like this:

<div class="mb-3">
  <label for="first_name" class="form-label">First Name</label>
  <input type="text" class="form-control {!! $errors->has('first_name') ? 'is-invalid' : '' !!}" id="first_name">
  {!! $errors->first('first_name', '<div class="invalid-feedback">:message</div>') !!}
</div>

And reduces it to this:

{!! BootForm::text('First Name', 'first_name') !!}

…with the is-invalid class being added automatically if there is an error in the session.

Horizontal Forms

To use a horizontal form instead of the standard basic form, simply swap the BootForm::open() call with a call to openHorizontal($columnSizes) instead:

// Width in columns of the left and right side
// for each breakpoint you’d like to specify.
$columnSizes = [
  'sm' => [4, 8],
  'lg' => [2, 10]
];

{!! BootForm::openHorizontal($columnSizes) !!}
  {!! BootForm::text('First Name', 'first_name') !!}
  {!! BootForm::text('Last Name', 'last_name') !!}
  {!! BootForm::text('Date of Birth', 'date_of_birth') !!}
  {!! BootForm::email('Email', 'email') !!}
  {!! BootForm::password('Password', 'password') !!}
  {!! BootForm::submit('Submit') !!}
{!! BootForm::close() !!}

Additional Tips

Hiding Labels

You can hide labels by chaining the hideLabel() helper off of any element definition.

BootForm::text('First Name', 'first_name')->hideLabel()

The label will still be generated in the markup, but hidden using Bootstrap’s .visually-hidden class, so you don’t reduce the accessibility of your form.

Form Text

You can add a text block underneath a form element using the formText() helper.

BootForm::text('Password', 'password')->formText('A strong password should be long and hard to guess.')

Model Binding

BootForms makes it easy to bind an object to a form to provide default values. Read more about it here.

BootForm::open()->action(route('users.update', $user))->put()
BootForm::bind($user)
BootForm::close()

typicms/bootforms 适用场景与选型建议

typicms/bootforms 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 59.77k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2018 年 05 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 typicms/bootforms 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 typicms/bootforms 我们能提供哪些服务?
定制开发 / 二次开发

基于 typicms/bootforms 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 59.77k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 12
  • 点击次数: 20
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 12
  • Watchers: 2
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-05-09