artisaninweb / laravel-formvalidation-helper
一个简化Laravel表单验证的助手。
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-24 02:18:51 UTC
README
一个简化Laravel表单验证的助手。
安装
将artisaninweb/laravel-formvalidation-helper
添加到composer.json的要求中
{ "require": { "artisaninweb/laravel-formvalidation-helper": "0.1.*" } }
将'Illuminate\Html\HtmlServiceProvider'
替换为'Artisaninweb\FormValidationHelper\ServiceProvider'
在别名中,将'Form' => 'Illuminate\Support\Facades\Form'
替换为'Form' => 'Artisaninweb\FormValidationHelper\Facades\Form'
用法
使用参数
required
(布尔值): 使字段必填。
rules
(字符串): 指定验证规则。
error-class
(字符串): 在表单字段出错时添加自定义类(可选,默认:'error')。
Form::text('field-name','field-value',[required' => true, 'rules' => 'required|email', 'error-class' => 'form-error']);
要输出错误,可以在视图中放置此代码。
默认情况下,错误将来自验证器,您可以在/app/lang/{lang}/validation.php
中编辑此内容。
// Replace the error with you own. Form::error('field-name','<p>You can put a custom html error here.</p>'); // Only replace the html tags Form::error('field-name','<p>:message</p>');
示例
echo Form::open(['url' => '/login']); echo Form::text('email', '', ['required' => true, 'rules' => 'required|email', 'error-class' => 'form-error']); echo Form::error('email', '<p>This field is required.</p>'); echo Form::password('password', '', ['required' => true, 'rules' => 'required|min:8', 'error-class' => 'form-error']); echo Form::error('password', '<p>This field is required.</p>'); echo Form::submit('Login'); echo Form::close();
在表单提交后,您可以验证最后提交的表单。
Form::validate(function($postData,$passes,$messages) { var_dump($postData); var_dump($passes); var_dump($messages); });
TwigBridge
如果您使用https://github.com/rcrowe/TwigBridge
作为Laravel的TwigBridge(就像我一样)。
您可以将'TwigBridge\Extension\Laravel\Form'
替换为'Artisaninweb\FormValidationHelper\Extension\TwigBridgeForm'
。
您可以在app/config/packages/rcrowe/twigbridge/extensions
中找到此内容。