barbuslex / bootforms
仅是一个带有一些Bootstrap特定便利性的表单构建器。记住旧输入,检索错误消息并自动处理所有你的Bootstrap模板标记。(基于adamwathan/bootforms)
Requires
- php: >=5.3.0
- adamwathan/form: v0.2
Requires (Dev)
- mockery/mockery: dev-master@dev
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2024-09-24 07:02:49 UTC
README
基于adamwathan/bootforms,修复了错误并进行了Laravel 4规范化
使用
{{ BootForm::text(name, value) }}
代替
{{ BootForm::text(value, name) }}
BootForms建立在更通用的Form包之上,通过添加另一个抽象层来快速生成标准Bootstrap 3表单的标记。可能不适合你的超级定制品牌即将发布的应用程序,但在原型设计阶段可以节省大量时间!
使用Composer安装
您可以通过在您的composer.json
中包含以下内容来安装此包:
{ "require": { "barbuslex/bootforms": "1.0" } }
注意:您也可以要求dev-master
来测试最新版本,但请确保将minimum-stability
设置为dev
。
Laravel 4
如果您正在使用Laravel 4,可以通过注册包含的服务提供程序来快速开始。
修改app/config/app.php
中的providers
数组以包含BootFormsServiceProvider
'providers' => array( //... 'AdamWathan\BootForms\BootFormsServiceProvider' ),
将BootForm
外观添加到app/config/app.php
中的aliases
数组
'aliases' => array( //... 'BootForm' => 'AdamWathan\BootForms\Facades\BootForm' ),
现在您可以通过直接调用BootForm
外观的方法来开始使用BootForms
BootForm::text('email', 'Email');
Laravel 4之外
在Laravel 4之外的使用稍微复杂一些,因为您需要建立一些依赖栈,但这并不太复杂。
$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 4设计的实现时,您必须提供自己的
AdamWathan\Form\OldInputInterface
和AdamWathan\Form\ErrorStoreInterface
实现。
使用BootForms
减少模板代码
典型的Bootstrap表单模板代码可能看起来像这样
<form> <div class="form-group"> <label class="control_label" for="first_name">First Name</label> <input type="text" class="form-control" id="first_name"> </div> <div class="form-group"> <label class="control_label" for="last_name">Last Name</label> <input type="text" class="form-control" id="last_name"> </div> <div class="form-group"> <label class="control_label" for="date_of_birth">Date of Birth</label> <input type="date" class="form-control" id="date_of_birth"> </div> <div class="form-group"> <label class="control_label" for="email">Email address</label> <input type="email" class="form-control" id="email"> </div> <div class="form-group"> <label class="control_label" for="password">Password</label> <input type="password" class="form-control" id="password"> </div> <button type="submit" class="btn btn-default">Submit</button> </form>
使用Laravel 4的FormBuilder
,您通常可以将它缩减到这样
{{ Form::open() }} <div class="form-group"> {{ Form::label('first_name', 'First Name', array('class' => 'control_label')) }} {{ Form::text('first_name', null, array('class' => 'form-control')) }} </div> <div class="form-group"> {{ Form::label('last_name', 'Last Name', array('class' => 'control_label')) }} {{ Form::text('last_name', null, array('class' => 'form-control')) }} </div> <div class="form-group"> {{ Form::label('date_of_birth', 'Date of Birth', array('class' => 'control_label')) }} {{ Form::text('date_of_birth', null, array('class' => 'form-control')) }} </div> <div class="form-group"> {{ Form::label('email', 'Email', array('class' => 'control_label')) }} {{ Form::email('email', null, array('class' => 'form-control')) }} </div> <div class="form-group"> {{ Form::label('password', 'Password', array('class' => 'control_label')) }} {{ Form::password('password', array('class' => 'form-control')) }} </div> {{ Form::submit('Submit', array('class' => 'btn btn-default')) }} {{ Form::close() }}
BootForms为您做了一些决定,并允许您进一步缩减它
{{ BootForm::open() }} {{ 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() }}
自动验证状态
BootForms的另一个优点是,如果它看到错误存储中的该控件有错误,它将自动将错误状态和错误消息添加到您的控件中。
基本上,这将使代码看起来像这样
<div class="form-group {{ $errors->has('first_name') ? 'has-error' : '' }}"> <label class="control_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()
调用即可
// Width in columns of the left and right side $labelWidth = 2; $controlWidth = 10; {{ BootForm::openHorizontal($labelWidth, $controlWidth) }} {{ 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() }}
自定义表单元素
如果您需要以任何方式自定义表单元素(例如向文本元素添加默认值或占位符),只需链式调用您需要的调用即可,它们将传递到底层的表单元素
// <div class="form-group"> // <label class="control_label" for="first_name">First Name</label> // <input type="text" class="form-control" id="first_name" placeholder="John Doe"> // </div> BootForm::text('first_name', 'First Name')->placeholder('John Doe');
有关更多信息,请查看我的基本Form包的文档。