activismebe/laravel-form-helpers

以简洁、便捷的方式处理表单模型绑定、旧输入绑定和验证错误信息。

1.2.0 2017-11-17 23:32 UTC

This package is not auto-updated.

Last update: 2024-09-15 02:48:19 UTC


README

一组blade指令,可自动填充表单,使用旧输入Eloquent模型,它还可以帮助您以干净、简单的方式显示验证错误信息

示例

看看使用这些指令是多么容易实现酷炫的功能,例如如果您使用Bootstrap进行标记,您可以这样做

<form action="/users" method="POST">

    @form($model)
    
    <div class="form-group @error('name', 'has-error')">
        <input type="text" @input('name')>
        @error('name')
    </div>
    
</form>

如果用户返回错误,结果将是

<form action="/users" method="POST">

    <div class="form-group has-error">
        <input type="text" name="name" value="Bad Name">
        <div class="help-block">Error message</div>
    </div>
    
</form>

太酷了!

安装

使用composer安装,只需运行以下命令

composer require sahibalejandro/laravel-form-helpers

然后添加服务提供者到您的config/app.php文件

'providers' => [
    Sahib\Form\FormServiceProvider::class,
];

这就完成了。

配置

可选地,您可以使用此命令发布配置文件

php artisan vendor:publish --provider=Sahib\Form\FormServiceProvider

之后,您可以编辑config/form-helpers.php文件。

使用方法

@form

@form([ Model $model = null ])

使用可选的@form指令将模型绑定到您的表单。
如果您只想进行old input绑定而不是模型绑定,则忽略此指令。

<form action="/users/123" method="POST">
    @form($user)
</form>

@input

@input(string $attribute [, string $default = null ])

使用@input指令将值分配给输入字段

<input type="text" @input('name')>
<input type="text" @input('something', 'default')>

这将产生以下标记

<input type="text" name="name" value="">
<input type="text" name="something" value="default"> 

@text

@text(string $attribute [, string $default = null ])

使用@text指令将值分配给文本区域字段

<textarea name="description">@text('description')</textareas>
<textarea name="bio">@text('bio', 'Default')</textareas>

这将产生以下标记

<textarea name="description"></textarea>
<textarea name="bio">Default</textarea>

@checkbox

@checkbox(string $attribute [, mixed $value = 1 [, boolean $checked = false ]])

使用@checkbox来设置复选框的值和状态

<input type="checkbox" @checkbox('remember_me')>

<!-- With a custom value -->
<input type="checkbox" @checkbox('newsletter', 'yes')>

<!-- Activate the checkbox by default -->
<input type="checkbox" @checkbox('send_sms', 1, true)>

这将产生以下标记

<input type="checkbox" name="remember_me" value="1">

<!-- With a custom value -->
<input type="checkbox" name="newsletter" value="yes">

<!-- Activate the checkbox by default -->
<input type="checkbox" name="send_sms" value="1" checked>

@radio

@radio(string $attribute [, mixed $value = 1 [, boolean $checked = false ]])

@radio指令的使用方式与@checkbox指令相同,实际上它只是一个别名

<input type="radio" @radio('color', 'red')>
<input type="radio" @radio('color', 'green', true)>
<input type="radio" @radio('color', 'blue')>

这将产生以下标记

<input type="radio" name="color" value="red">
<input type="radio" name="color" value="green" checked>
<input type="radio" name="color" value="blue">

@options

@options(array $options, string $attribute [, mixed $default = null [, string $placeholder = null ]])

使用@options指令显示下拉列表中的选项。

注意:当模型的属性、旧输入或$default值是数组时,它还适用于多选下拉列表字段。

假设我们传递一个名为$cardTypes的数组到视图中,并使用@options指令

$cardTypes = [
    'VISA' => 'Visa',
    'MC'   => 'Master Card',
    'AME'  => 'American Express',
];
<select name="card_type">
    @options($cardTypes, 'card_type')
</select>

这将产生以下标记

<select name="card_type">
    <option value="VISA">Visa</option>
    <option value="MC">Master Card</option>
    <option value="AME">American Express</option>
</select>

当然,您可以设置一个默认选中的选项

<select name="card_type">
    @options($cardTypes, 'card_type', 'MC')
</select>

结果将是

<select name="card_type">
    <option value="VISA">Visa</option>
    <option value="MC" selected>Master Card</option>
    <option value="AME">American Express</option>
</select>

您还可以定义一个占位符选项

<select name="card_type">
    @options($cardTypes, 'card_type', null, 'Select a card type')
</select>

结果将是

<select name="card_type">
    <option value="" selected disabled>Select a card type</option>
    <option value="VISA">Visa</option>
    <option value="MC">Master Card</option>
    <option value="AME">American Express</option>
</select>

@error

@error(string $attribute [, string $template = null ])

使用@error指令显示验证错误信息,此指令将为您检查是否存在错误。

<input type="text" @input('name')>
@error('name')

然后当用户带错误返回时,结果将是

<input type="text" name="name" value="Name That Fail Validation">
<div class="help-block">The name field fails validation.</div>

请注意,@error 指令默认是 Bootstrap 兼容的,但您可以在行内或配置文件中定义自定义模板。

@error('name', '<span class="error">:message</span>')

结果将是

<span class="error">Error message</span>

看看使用 @error 指令完成酷炫功能有多简单,例如,如果您使用 Bootstrap 进行标记,您可以这样做:

<div class="form-group @error('name', 'has-error')">
    <input type="text" @input('name')>
    @error('name')
</div>

如果用户带有错误被重定向回来,结果将是:

<div class="form-group has-error">
    <input type="text" name="name" value="Bad Name">
    <div class="help-block">Error message</div>
</div>