laravel validator使用(转载)
2015-03-19 14:11
316 查看
原 10. Laravel 4 验证
发表于1年前(2013-12-21 23:00) 阅读(1703) | 评论(0) 3人收藏此文章, 我要收藏赞1
Team 文档协作功能重磅推出,你讨厌写文档吗?
laravel validator
目录[-]
如何进行验证
可用的验证规则
regex:pattern(正则)
accepted(yes|no|1)
in:foo,bar,…(in 规则)
notin:_foo,bar,…(not in 规则)
alpha(全部字母)
numeric(数字)
alpha_num(字母|数字)
alpha_dash(字母|数字|中划线|下划线)
url(合法 URL,不严谨)
active_url(合法 URL,基于 checkdnsrr)
email(电子邮件)
image (jpeg|png|bmp|gif)
integer(整数)
ip(IP 地址)
before:date(给定日期之前)
after:date(给定日期之后)
between:min,max(min 和 max 之间)
confirmed(二次确认域,如“密码的二次确认域”)
same:field(值与给定域相同)
size:value(大小与给定域的值相同)
date(合法日期字符串,基于 strtotime)
dateformat:_format(format 格式 y-m-d H:i:s)
different:field(不同于)
min:value(最小值)
max:value(最大值)
mimes:foo,bar,…(MIME 类型限制)
MIME 规则的基础使用
required(必填)
requiredif:_field,value(当指定域为某个值时,必填)
requiredwith:_foo,bar,…(当指定域存在时,必填)
requiredwithout:_foo,bar,…(当指定域不存在时,必填)
exists:table,column(必须存在于 table 表的 column 字段数据中)
Exists 规则的基础使用
unique:table,column,except,idColumn(数据库唯一)
Unique 规则的基础使用
如何获取错误消息
构造为 JSON 数据(API)
重定向,直接传递至下一个页面
视图中如何获取传递过来的错误消息
获取一个域的第一个错误消息
获取一个域的全部错误消息
获取全部域的全部错误消息
检查一个域是否存在消息
以某种格式获取一条错误消息
以某种格式获取所有错误消息
定制验证规则
定制验证消息
如何进行验证
典型的注册表单:<!-- app/views/form.blade.php --> {{ Form::open(array('url' => 'registration')) }} {{-- Username field. ------------------------}} {{ Form::label('username', 'Username') }} {{ Form::text('username') }} {{-- Email address field. -------------------}} {{ Form::label('email', 'Email address') }} {{ Form::email('email') }} {{-- Password field. ------------------------}} {{ Form::label('password', 'Password') }} {{ Form::password('password') }} {{-- Password confirmation field. -----------}} {{ Form::label('password_confirmation', 'Password confirmation') }} {{ Form::password('password_confirmation') }} {{-- Form submit button. --------------------}} {{ Form::submit('Register') }} {{ Form::close() }}
路由部分:
// app/routes.php Route::get('/', function() { return View::make('form'); }); Route::post('/registration', function() { // 获取所有表单数据. $data = Input::all(); // 创建验证规则 $rules = array( 'username' => array('alpha_num', 'min:3') ); // 开始验证 $validator = Validator::make($data, $rules); if ($validator->passes()) { // 验证成功 return 'Data was saved.'; } // 验证失败 return Redirect::to('/'); });
可用的验证规则
regex:pattern(正则)
验证此规则的值必须符合给定的正则表达式。accepted(yes|no|1)
验证此规则的值必须是 yes、 on 或者是 1。这在验证是否同意"服务条款"的时候非常有用。in:foo,bar,…(in 规则)
验证此规则的值必须在给定的列表中存在。notin:_foo,bar,…(not in 规则)
验证此规则的值必须在给定的列表中不存在。alpha(全部字母)
验证此规则的值必须全部由字母字符构成。numeric(数字)
验证此规则的值必须是一个数字。alpha_num(字母|数字)
验证此规则的值必须全部由字母和数字构成。alpha_dash(字母|数字|中划线|下划线)
验证此规则的值必须全部由字母、数字、中划线或下划线字符构成。url(合法 URL,不严谨)
验证此规则的值必须是一个合法的 URL。注意: 已经证实此规则不严谨,类似
2http://url.com的 URL 可以通过验证。
active_url(合法 URL,基于 checkdnsrr)
验证此规则的值必须是一个合法的 URL,根据 PHP 函数 checkdnsrr。注意: 由于是基于 checkdnsrr 因此也可用于验证邮箱地址是否存在。
email(电子邮件)
验证此规则的值必须是一个合法的电子邮件地址。image (jpeg|png|bmp|gif)
验证此规则的值必须是一个图片 (jpeg, png, bmp 或者 gif)。integer(整数)
验证此规则的值必须是一个整数。ip(IP 地址)
验证此规则的值必须是一个合法的 IP 地址。before:date(给定日期之前)
验证此规则的值必须在给定日期之前,日期将通过 PHP 函数 strtotime 传递。after:date(给定日期之后)
验证此规则的值必须在给定日期之后,日期将通过 PHP 函数 strtotime 传递。between:min,max(min 和 max 之间)
验证此规则的值必须在给定的 min 和 max 之间。字符串、数字以及文件都将使用大小规则进行比较。confirmed(二次确认域,如“密码的二次确认域”)
验证此规则的值必须和 foo_confirmation 的值相同。比如,需要验证此规则的域是 password,那么在输入中必须有一个与之相同的 password_confirmation 域。same:field(值与给定域相同)
验证此规则的值必须与给定域的值相同。size:value(大小与给定域的值相同)
验证此规则的值的大小必须与给定的 value 相同。对于字符串,value 代表字符的个数,
对于数字,value 代表它的整数值,
对于文件,value 代表文件以KB为单位的大小。
date(合法日期字符串,基于 strtotime)
验证此规则的值必须是一个合法的日期,根据 PHP 函数 strtotime。dateformat:_format(format 格式 y-m-d H:i:s)
验证此规则的值必须符合给定的 format 的格式,根据 PHP 函数 date_parse_from_format。different:field(不同于)
验证此规则的值必须与指定的 field 域的值不同。min:value(最小值)
验证此规则的值必须大于最小值 value。字符串、数字以及文件都将使用大小规则进行比较。max:value(最大值)
验证此规则的值必须小于最大值 value。字符串、数字以及文件都将使用大小规则进行比较。mimes:foo,bar,…(MIME 类型限制)
验证此规则的文件的 MIME 类型必须在给定的列表中。MIME 规则的基础使用
'photo' => 'mimes:jpeg,bmp,png'
注意: 当使用 regex 模式的时候,有必要使用数组指定规则,而不是管道分隔符,特别是正则表达式中包含一个管道字符的时候。
required(必填)
验证此规则的值必须在输入数据中存在。requiredif:_field,value(当指定域为某个值时,必填)
当指定的域为某个值的时候,验证此规则的值必须存在。requiredwith:_foo,bar,…(当指定域存在时,必填)
仅当指定的域存在的时候,验证此规则的值必须存在。requiredwithout:_foo,bar,…(当指定域不存在时,必填)
仅当指定的域不存在的时候,验证此规则的值必须存在。exists:table,column(必须存在于 table 表的 column 字段数据中)
验证此规则的值必须在指定的数据库的表中存在。Exists 规则的基础使用
'state' => 'exists:states'
指定列名
'state' => 'exists:states,abbreviation'
您也可以指定更多的条件,将以 “where” 的形式添加到查询。
'email' => 'exists:staff,email,account_id,1'
unique:table,column,except,idColumn(数据库唯一)
验证此规则的值必须在给定的数据库的表中唯一。如果 column 没有被指定,将使用该域的名字。Unique 规则的基础使用
'email' => 'unique:users'
指定列名
'email' => 'unique:users,email_address'
强制忽略一个给定的 ID
'email' => 'unique:users,email_address,10'
添加额外的where语句
你还可以指定更多条件,这些条件将被添加到查询的"where"语句中:
'email' => 'unique:users,email_address,NULL,id,account_id,1
在上面的规则中,只有account_id 为 1 的行才会被包含到unique检查中。
如何获取错误消息
构造为 JSON 数据(API)
Route::post('/registration', function() { // 获取全部提交数据 $data = Input::all(); // 构造规则数组 $rules = array( 'username' => 'alpha_num' ); // 开始验证 $validator = Validator::make($data, $rules); if ($validator->passes()) { // 验证通过 return 'Data was saved.'; } // 验证失败 // 获取错误消息 $errors = $validator->messages(); // 构造 JSON 响应 return Response::json($errors); });
重定向,直接传递至下一个页面
return Redirect::to('/')->withErrors($validator);
视图中如何获取传递过来的错误消息
注意:
$errors是系统预定义变量,任何模板中均可使用。
获取一个域的第一个错误消息
{{ $errors->first('username') }}
获取一个域的全部错误消息
@foreach($errors->get('username') as $message) <li>{{ $message }}</li> @endforeach
获取全部域的全部错误消息
@foreach($errors->all() as $message) <li>{{ $message }}</li> @endforeach
检查一个域是否存在消息
@if($errors->has('email')) <p>Yey, an error!</p> @endif
以某种格式获取一条错误消息
{{ $errors->first('username', '<span class="error">:message</span>') }}
注意: 默认情况下,消息将使用与 Bootstrap 兼容的语法进行格式化。
以某种格式获取所有错误消息
@foreach($errors->all('<li>:message</li>') as $message) {{ $message }} @endforeach
定制验证规则
注意: 匿名函数 和 指向普通类方法 的 规则扩展 并不推荐,因此直接介绍如何直接扩展官方的验证器。编写扩展验证器类:
class ExValidator extends Illuminate\Validation\Validator { // 规则 public function validateFoo($attribute, $value, $parameters) { return $value == 'foo'; } // 消息 protected function replaceFoo($message, $attribute, $rule, $parameters) { return str_replace(':foo', $parameters[0], $message); } }
注册定制的验证器扩展:
Validator::resolver(function($translator, $data, $rules, $messages) { return new ExValidator($translator, $data, $rules, $messages); });
详细的编写方法请参照官方类
/vendor/laravel/framework/src/Illuminate/Validation/Validator.php。
定制验证消息
针对某一个验证规则:$messages = array( 'required' => 'The :attribute field is required.', ); $validator = Validator::make($input, $rules, $messages);
针对指定域的某一规则:
$messages = array( 'email.required' => 'We need to know your e-mail address!', );
在语言文件中定义
/app/lang/zh-CN/validation.php:
'custom' => array( 'email' => array( 'required' => '请填写您的 email 地址。', ), ),
相关文章推荐
- 使用JDBC存储和调用大对象[绝对转载]
- 转载:使用第三方软件的痛苦经历
- 使用open source产品组装你的web应用架构(二) 转载
- TPC基准程序及tpmc值-兼谈在使用性能度量时如何避免误区[转载]
- [转载] 使用 Java Web Start 部署 SWT 应用程序
- 用javascript操作xml-->Web设计中如何使用XML数据源对象(转载)
- 使用open source产品组装你的web应用架构(三) 转载
- 在WebWork中使用组件(转载)
- 使用C#创建webservice及三种调用方式(转载)
- 转载--Struts1.1 validation framework 使用中的若干经验
- (转载)使用ActiveX控件开发网页常见的问题
- 强烈建议PHPer们使用Turck MMCache (转载)
- 使用JFreeChart创建图象【转载】
- (转载)使用 COM 符号引擎辅助调试
- VS.NET使用小经验 (转载)
- [Visual C++]在Win2000/XP上安静地替换正在使用的系统文件【转载/收藏】
- (转载)使用ActiveX控件开发网页常见的问题
- 使用open source产品组装你的web应用架构(四) 转载
- Oracle SQL*Loader 使用指南(转载)
- [转载] 在Eclipse中使用SWT进行界面设计