blip / bootstrap-form
Laravel 9 的 Bootstrap 5 表单包装器。
Requires
- php: ^8.0|^8.1
- illuminate/config: ~8.0|^9.0
- illuminate/session: ~8.0|^9.0
- illuminate/support: ~8.0|^9.0
- laravelcollective/html: ^6.0
Requires (Dev)
- mockery/mockery: ~1.4.1
- phpunit/phpunit: ~9.5
This package is auto-updated.
Last update: 2024-09-06 20:07:04 UTC
README
这是一个用于在 Laravel 8 & 9 中简单地创建 Bootstrap 5 样式的表单组的包。它扩展了正常的表单构建器,为您提供了带有标签、错误信息和适当类使用的水平表单组。
简介
当您想要生成 Bootstrap 3、4 & 5 表单组时,只需将 BootstrapForm
门面用于 Form
门面。
BootForm::text('username');
您将得到以下内容
<div class="form-group"> <label for="username" class="control-label col-md-2">Username</label> <div class="col-md-10"> <input type="text" name="username" class="form-control" /> </div> </div>
当然,如果该字段有错误,它甚至会填充它们。
<div class="form-group has-error"> <label for="username" class="control-label col-md-2">Username</label> <div class="col-md-10"> <input type="text" name="username" class="form-control" /> <span class="help-block">The username field is required.</span> </div> </div>
安装
首先,使用 Composer 需要此包。
composer require blip/bootstrap-form
现在,将这些服务提供者添加到您的 config/app.php
文件中(如果您已经有 HtmlServiceProvider
,则无需添加)。
Collective\Html\HtmlServiceProvider::class, Blip\BootstrapForm\BootstrapFormServiceProvider::class,
最后,将这些添加到别名数组中(注意:Form 和 Html 必须在 BootstrapForm 之前列出)
'Form' => Collective\Html\FormFacade::class, 'HTML' => Collective\Html\HtmlFacade::class, 'BootForm' => Blip\BootstrapForm\Facades\BootstrapForm::class,
如果您更喜欢更短的别名,也可以自由地为 BootstrapForm 使用不同的别名。
配置
BootstrapForm 提供了许多配置选项。运行以下 Artisan 命令以将配置选项发布到您的 config
目录
php artisan vendor:publish
水平表单大小
当使用水平表单时,您可以在下面指定左列和右列的默认大小。请注意,您可以指定每个列的任意多个类,以改善移动端响应性,例如
col-md-3 col-sm-6 col-xs-12
显示错误
默认情况下,此包将仅显示每个字段的第一个验证错误。如果您想列出字段的全部验证错误,只需将此配置选项设置为 true。
用法
打开表单
BootstrapForm 改进了打开表单的过程,无论是在提供 Bootstrap 类还是在管理基于模型的表单模型方面。
// Passing an existing, persisted model will trigger a model // binded form. $user = User::whereEmail('example@example.com')->first(); // Named routes BootForm::open(['model' => $user, 'store' => 'users.store', 'update' => 'users.update']); // Controller actions BootForm::open(['model' => $user, 'store' => 'UsersController@store', 'update' => 'UsersController@update']);
如果将模型传递给 open 方法,它将配置为使用 update
路由和 PUT
方法。否则,它将指向 store
方法作为 POST
请求。这样,您可以使用相同的打开标签处理创建和保存。
// Passing a model that hasn't been saved or a null value as the // model value will trigger a `store` form. $user = new User; BootForm::open()
带有参数的路由
如果路由包含参数,您可以通过替换路由或操作名称字符串为数组来传递它们。第一个条目是路由名称的字符串,后面是您传递给 route
函数的参数。
BootForm::open(['update' => ['posts.comments.create', $post]])
表单变体
有一些助手用于打开不同类型的 Bootstrap 表单。默认情况下,open()
将使用配置文件中设置的表单样式。这些助手与 open()
方法接受相同的输入。
// Open a vertical Bootstrap form. BootForm::vertical(); // Open an inline Bootstrap form. BootForm::inline(); // Open a horizontal Bootstrap form. BootForm::horizontal();
如果您想更改表单的列以偏离配置文件中的设置,您也可以通过 $options
数组来设置它们。
BootForm::open(['left_column_class' => 'col-md-2', 'left_column_offset_class' => 'col-md-offset-2', 'right_column_class' => 'col-md-10']);
文本输入
这里有一些文本输入的方法。请注意,方法签名与 Laravel 表单构建器提供的方法签名相对接近,但需要一个表单标签的参数。
// The label will be inferred as 'Username'. BootForm::text('username'); // The field name by default is 'email'. BootForm::email(); BootForm::textarea('profile'); // The field name by default is 'password'. BootForm::password();
复选框和单选按钮输入
复选框和单选按钮略有不同,并生成不同的标记。
查看配置选项的方法签名。
// A checked checkbox. BootForm::checkbox('interests[]', 'Laravel', 'laravel', true);
单选输入也是如此。
BootForm::radio('gender', 'Male', 'male');
多个复选框和单选按钮
通过简单地传递一组值/标签对,您可以轻松地生成一组复选框或单选按钮。
$label = 'this is just a label'; $interests = [ 'laravel' => 'Laravel', 'rails' => 'Rails', 'ie6' => 'Internet Explorer 6' ]; // Checkbox inputs with Laravel and Rails selected. BootForm::checkboxes('interests[]', $label, $interests, ['laravel', 'rails']); $genders = [ 'male' => 'Male', 'female' => 'Female' ]; // Gender inputs inline, 'Gender' label inferred. BootForm::radios('gender', null, $genders, null, true); // Gender inputs with female selected. BootForm::radios('gender', 'Gender', $genders, 'female');
提交按钮
// Pretty simple. BootForm::submit('Login');
自定义类
// Pretty simple. BootForm::submit('Login', ['class'=>'btn btn-success']);
// Pretty simple. BootForm::button('Login');
关闭表单
// Pretty simple. BootForm::close();
标签
隐藏标签
您可以通过将值设置为 false
来隐藏元素的标签。
// An input with no label. BootForm::text('username', false);
带有 HTML 的标签
要在标签中包含 HTML 代码
// A label with HTML code using array notation BootForm::text('username', ['html' => 'Username <span class="required">*</span>']); // A label with HTML code using HtmlString object BootForm::text('username', new Illuminate\Support\HtmlString('Username <span class="required">*</span>'));
帮助文本
您可以将 help_text
选项传递给任何字段,以便将 Bootstrap 帮助文本 添加到渲染的表单组中。
表单输入组(后缀和前缀)
为任何输入添加前缀和/或后缀 - 您可以添加文本、图标和按钮。
// Suffix button with 'Call' as label and success class to button {!! BootForm::text('tel', 'Phone', null, ['suffix' => BootForm::addonButton('Call', ['class' => 'btn-success'])] ) !!} // Prefix button with 'Call' as label and success class to button {!! BootForm::text('tel', 'Phone', null, ['prefix' => BootForm::addonButton('Call', ['class' => 'btn-success'])] ) !!} // Prefix icon (I put second parameter after <i class="fa fa-SECOND_PARAMETER"></i>) with 'dollar' as icon {!! BootForm::text('tel', 'Phone', null, ['prefix' => BootForm::addonIcon('dollar')] ) !!} // Prefix and suffix as text {!! BootForm::text('tel', 'Phone', null, ['prefix' => BootForm::addonText('1-'), 'suffix' => BootForm::addonIcon('phone')] ) !!} // Prefix and suffix with button {!! BootForm::text('tel', 'Phone', null, ['suffix' => BootForm::addonButton('Boom!', ['class' => 'btn-danger']), 'prefix' => BootForm::addonButton('Call', ['class' => 'btn-success'])] ) !!}