talentasia/bootforms

具有一些Bootstrap特定便利性的表单构建器。

0.1 2017-08-03 05:24 UTC

This package is auto-updated.

Last update: 2024-09-26 16:00:27 UTC


README

使用Composer安装

您可以通过在项目的根目录下终端中运行此命令来通过Composer安装此包

composer require talentasia/bootforms

Laravel

如果您正在使用Laravel 4或5,可以通过注册包含的服务提供程序快速开始。

修改providers数组中的config/app.php以包含BootFormsServiceProvider

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

BootForm外观添加到config/app.php中的aliases数组

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

现在您可以通过直接调用BootForm外观的方法来开始使用BootForms

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

Laravel之外

在Laravel之外使用稍微复杂一些,因为您需要构建一定的依赖栈,但并不太复杂。

$formBuilder = new TalentAsia\Form\FormBuilder;

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

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

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

注意:当不使用为Laravel设计的实现时,您必须提供自己的TalentAsia\Form\OldInputInterfaceTalentAsia\Form\ErrorStoreInterface实现。

使用BootForms

基本用法

BootForms允许您通过一个调用创建一个标签和表单控件,并将它们全部包装在一个表单组中。

//  <form method="POST">
//    <div class="form-group">
//      <label for="field_name">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() !!}

注意:在尝试创建字段之前,不要忘记open()表单!BootForms需要在渲染字段之前知道您是打开了垂直表单还是水平表单,因此如果您忘记,将会得到一个错误。

自定义元素

如果您需要以任何方式自定义表单元素(例如,为文本元素添加默认值或占位符),只需链式调用所需的方法,它们将传递到底层的表单元素。

可以通过attribute方法添加属性,或者简单地使用属性名作为方法名。

// <div class="form-group">
//    <label for="first_name">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="form-group">
//   <label for="color">Color</label>
//   <select class="form-control" 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="form-group">
//    <label for="first_name">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');

减少样板代码

典型的Bootstrap表单样板代码可能看起来像这样

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

BootForms为您做了一些决定,并允许您进一步精简它

{!! 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() !!}

自动验证状态

BootForms的另一个优点是,如果它在错误存储中看到该控件有错误,它会自动将错误状态和错误消息添加到您的控件中。

基本上,这将代码简化如下

<div class="form-group {!! $errors->has('first_name') ? 'has-error' : '' !!}">
  <label for="first_name">First Name</label>
  <input type="text" class="form-control" id="first_name">
  {!! $errors->first('first_name', '<p class="help-block">:message</p>') !!}
</div>

并减少到这个

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

...如果会话中存在错误,则会自动添加has-error类。

水平表单

要使用水平表单而不是标准基本表单,只需将BootForm::open()调用替换为对openHorizontal($columnSizes)的调用即可

// 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() !!}

其他技巧

隐藏标签

您可以通过链式调用任何元素定义的hideLabel()辅助器来隐藏标签。

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

标签仍然会在标记中生成,但会使用Bootstrap的.sr-only类隐藏,这样就不会降低您表单的可访问性。

帮助块

您可以使用helpBlock()辅助器在表单元素下方添加一个帮助块。

BootForm::text('Password', 'password')->helpBlock('一个强大的密码应该足够长且难以猜测。')

注意:如果存在验证错误,此帮助块将自动被错误覆盖。

模型绑定

BootForms使绑定对象到表单以提供默认值变得容易。

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

相关资源

本包基于 https://github.com/adamwathan/bootforms