enriquejlicona/bootforms

带有Bootstrap特定便利性的表单构建器。记住旧输入,检索错误消息,并自动处理所有你的Bootstrap模板标记。

1.6.1 2022-03-04 20:55 UTC

README

Build Status Coverage Status

BootForms

BootForms是一个Laravel包,可以快速生成标准Bootstrap 3表单的标记。可能不适合你的超级定制品牌发布应用,但在原型设计阶段可以大大节省时间!

查看Aire

此包已被Aire取代,Aire是一个具有相似功能但API改进、更多测试、更多文档和非Bootstrap主题支持的现代表单构建器。它默认使用Tailwind,但通过插件完全支持Bootstrap。

BootForms不再积极维护。我会尝试合并任何可预见的未来修复的bug,但我强烈建议您查看Aire。

分支

此包分支了Adam Wathan的存储库,以支持更新的Laravel版本以及原包的更改

  • 支持包自动发现
  • 改进了对IDE自动补全的支持(使用laravel-ide-helper
  • 改进了对模型绑定的支持
  • 转变为仅Laravel包,以进一步简化体验
  • 停止支持Laravel 5.5之前的版本

目录

使用Composer安装

此包支持Laravel自动发现,因此您只需要在项目的根目录下终端中运行此命令

composer require galahad/bootforms

使用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()