sahibalejandro/laravel-form-helpers

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

1.3.2 2022-02-12 05:22 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:36:59 UTC


README

Build Status Latest Stable Version Total Downloads License

一组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指令将模型绑定到您的表单。
如果您只想进行旧输入绑定而不是模型绑定,请忽略此指令。

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