realripley00/bootstrap-4-form

Laravel 5 用于 Bootstrap 4 的表单包装器。

1.1.29 2018-12-03 02:05 UTC

README

这是 Dwight Watson 的 Bootstrap 3 表单构建器的分支,已更新/重新设计,以在 Laravel 5 应用中使用 Bootstrap 4 类等。它扩展了 Laravel Collective 表单构建器,为您提供带有标签、错误消息和适当类使用的水平表单组。

Total Downloads Monthly Downloads License

简介

当您想生成 Bootstrap 4 表单组时,只需在 Form 掩饰的地方使用 BootstrapForm 掩饰。如果您使用以下别名,它将看起来像这样:BootForm::open() 等。

BootForm::text('username');

您将得到以下内容

<div class="form-group">
    <label for="username" class="form-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="form-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>

如果您熟悉 Dwight Watson 的 Bootstrap 3 表单包,这基本上是同一件事,但用于 Bootstrap 4 表单。

安装

首先,使用 Composer 需要该包。

composer require realripley00/bootstrap-4-form

现在,将这些服务提供者添加到您的 config/app.php 文件中(如果您已经有 HtmlServiceProvider,则不要添加)。

Collective\Html\HtmlServiceProvider::class,
RealRipley\BootstrapForm\BootstrapFormServiceProvider::class,

最后,将这些添加到别名数组中(注意:Form 和 Html 必须在 BootstrapForm 之前列出)

'Form'     => Collective\Html\FormFacade::class,
'HTML'     => Collective\Html\HtmlFacade::class,
'BootForm' => RealRipley\BootstrapForm\Facades\BootstrapForm::class,

配置

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

表单变体

有几个助手可用于打开不同类型的 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',
    'react'   => 'React',
    'vue'     => 'Vue'
];

// 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::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'])] ) !!}

Dwight Watson原始的Bootstrap 3表单构建器

这是这个优秀包的分支,稍微修改了一下,以使用Bootstrap 4。