adamwathan / bootforms
一个带有一些Bootstrap特定便捷功能的表单构建器。记住旧输入,检索错误消息,并自动处理所有Bootstrap模板标记。
Requires
- php: >=5.4.0
- adamwathan/form: ^0.9.0
Requires (Dev)
- mockery/mockery: 0.9.*
- phpunit/phpunit: 3.7.*
- satooshi/php-coveralls: dev-master
This package is not auto-updated.
Last update: 2022-02-01 12:27:39 UTC
README
重要:此包不再积极维护。 对于错误修复和新功能,请进行分支。
BootForms
BootForms在我的更通用的表单包的基础上构建,通过添加另一层抽象来快速生成标准Bootstrap 3表单的标记。可能不适合您的超级定制品牌即将发布的应用程序,但您在原型设计阶段可以节省大量时间!
使用Composer安装
您可以通过在项目的根目录下终端中运行此命令来使用Composer安装此包:
composer require adamwathan/bootforms
Laravel
如果您正在使用Laravel 4或5,可以通过注册包含的服务提供商快速开始。
修改config/app.php
中的providers
数组以包含BootFormsServiceProvider
'providers' => [ //... 'AdamWathan\BootForms\BootFormsServiceProvider' ],
将BootForm
外观添加到config/app.php
中的aliases
数组
'aliases' => [ //... 'BootForm' => 'AdamWathan\BootForms\Facades\BootForm' ],
现在您可以通过调用BootForm
外观上的方法来开始使用BootForms
BootForm::text('Email', 'email');
Laravel之外
在Laravel之外使用稍微复杂一些,因为您需要建立一些依赖栈,但这并不复杂。
$formBuilder = new AdamWathan\Form\FormBuilder; $formBuilder->setOldInputProvider($myOldInputProvider); $formBuilder->setErrorStore($myErrorStore); $formBuilder->setToken($myCsrfToken); $basicBootFormsBuilder = new AdamWathan\BootForms\BasicFormBuilder($formBuilder); $horizontalBootFormsBuilder = new AdamWathan\BootForms\HorizontalFormBuilder($formBuilder); $bootForm = new AdamWathan\BootForms\BootForm($basicBootFormsBuilder, $horizontalBootFormsBuilder);
注意:当不使用为Laravel提供的实现时,您必须提供自己的
AdamWathan\Form\OldInputInterface
和AdamWathan\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()
相关资源
- Laravel Translatable BootForms,将BootForms与dimsav的Laravel Translatable包集成