actb/laravel-form-helpers

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

1.6.3 2020-03-09 19:59 UTC

This package is auto-updated.

Last update: 2024-09-08 10:17:48 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 actb/laravel-form-helpers

然后,将服务提供者添加到你的 config/app.php 文件中

'providers' => [
    ActivismeBE\FormHelper\FormServiceProvider::class,
];

就这些。

配置

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

php artisan vendor:publish --provider=ActivismeBE\FormHelper\FormServiceProvider

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

注意:此步骤仅在你应用版本低于 5.5 时需要

使用

@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')</textarea>
<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 值是数组时,它也适用于 select multiple 字段。

假设我们传递一个名为 $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>

扩展PhpStorm

为了让PhpStorm使用此包的定制blade指令,请按照以下步骤操作,并添加所需的内容。

  1. 在PhpStorm中打开首选项,然后转到语言和框架 -> PHP -> Blade(文件 | 设置 | 语言与框架 | PHP | Blade)
  2. 取消选中“使用默认设置”,然后点击指令选项卡。
  3. 为validation-helpers包添加以下新指令: