laravel 数据验证规则
return [
'accepted' => '必须为yes,on,1,true',
'active_url' => '是否是一个合法的url,基于PHP的checkdnsrr函数,因此也可以用来验证邮箱地址是否存在',
'after:date' => '验证字段必须是给定日期后的值,比如required|date|after:tomorrow,通过PHP函数strtotime来验证',
'after_or_equal:date' => '大于等于',
'alpha' => '验证字段必须全是字母',
'alpha_dash' => '验证字段可能具有字母、数字、破折号、下划线',
'alpha_num' => '验证字段必须全是字母和数字',
'array' => '数组',
'before:date' => '小于',
'before_or_equal:date' => '小于等于',
'between:min,max' => '给定大小在min,max之间,字符串,数字,数组或者文件大小都用size函数评估',
'boolean' => '必须为能转化为布尔值的参数,比如:true,false,1,0,"1","0"',
'confirmed' => '字段必须与foo_confirmation字段值一致,比如,要验证的是password,输入中必须存在匹配的password_confirmation字段',
'date' => '通过strtotime校验的有效日期',
'date_equals:date' => '等于',
'date_format:format' => 'date和date_format不应该同时使用,按指定时间格式传值',
'different:field' => '验证的字段值必须与字段field的值相同',
'digits:value' => '必须是数字,并且有确切的值',
'digits_between:min,max' => '字段长度必须在min,max之间',
'dimensions' => '验证的文件是图片并且图片比例必须符合规则,比如dimensions:min_width=100,min_height=200,可用
的规则有min_width,max_width,min_height,max_height,width,height,ratio',
'distinct' => '无重复值',
'email' => '符合e-mail地址格式',
'exists:table,column' => '必须存在于指定的数据库表中',
'file' => '成功上传的文件',
'filled' => '验证的字段存在时不能为空',
'image' => '验证的文件必须是图像,jpeg,png,bmp,gif,svg',
'in:foo,bar,...' => '验证的字段必须包含在给定的值列表中',
'in_array:anotherfield' => '验证的字段必须存在于另一个字段的值中',
'integer' => '整数',
'ip' => 'ip地址',
'ipv4' => 'ipv4地址',
'ipv6' => 'ipv6地址',
'json' => 'json字符串',
'max:value' => '大于',
'mimetypes:text/plain,...' => '验证的文件必须与给定的MIME类型匹配',
'mimes:foo,bar,...' => '验证的文件必须具有列出的其中一个扩展名对应的MIME类型',
'min:value' => '小于',
'nullable' => '可为null,可以包含空值的字符串和整数',
'not_in:foo,bar...' => '不包含',
'numeric' => '必须为数字',
'present' => '验证的字段必须存在于输入数据中,但可以为空',
'regex:pattern' => '验证的字段必须与给定正则表达式匹配',
'required' => '验证的字段必须存在于输入数据中,但不可以为空',
//以下情况视为空:1.该值为null,2.空字符串,3.空数组或空的可数对象,4.没有路径的上传文件
'required_if:anotherfield,value,...' => '如果指定的anotherfield等于value时,被验证的字段必须存在且不为空',
'required_unless:anotherfield,value,...' => '如果指定的anotherfield等于value时,被验证的字段不必存在',
'required_with:foo,bar,...' => '只要指定的其它字段中有任意一个字段存在,被验证的字段就必须存在且不为空',
'required_with_all:foo,bar,...' => '当指定的其它字段必须全部存在时,被验证的字段才必须存在且不为空',
'required_without_all:foo,bar,...' => '当指定的其它字段必须全部不存在时,被验证的字段必须存在且不为空',
'required_without:foo,bar,...' => '当指定的其它字段有一个字段不存在,被验证的字段就必须存在且不为空',
'same:field' => '给定字段必须与验证字段匹配',
'size:value' => '验证字段必须具有与给定值匹配的大小,对字符串,value对应字符数;对数字,对应给定的
整数值;对数组,对应count值;对文件,是文件大小(kb)',
'timezone' => '验证字段是有效的时区标识符,根据PHP函数timezone_identifiers_list判断',
'unique:table,column,except,idColumn' => '验证字段必须是数据库中唯一的',
'url' => '有效的url',
];
简单例子
return [
'title.required' => 'A title is required',
'body.required' => 'A message is required',
'avatar' => [
'required',
Rule::dimensions()->maxWidth(500)->maxHeight(250)->ratio(3/2), //限制图片大小和比例
],
'foo.*.id' =>'distinct', //不允许重复
'state' =>'exists:states', //指定表
'state1' => 'exists:states,abbreviation', //指定表和字段
'email' => 'exists:connection.staff,email', //指定查询的数据库
'email1' => [
'required',
Rule::exists('staff')->where(function ($query){
$query->where('account_id',1);
}),
],
'zones' => [
'required',
Rule::in(['first-zone','second-zone']),
],
'video' => 'mimetypes:video/avi,video/mpeg,video/quicktime',
'photo' => 'mimes:jpeg,bmp,png', //验证文件扩展名,规则上也会验证文件的MIME类型,通过读取文件的内容以猜测它的MIME类型
'toppings' => [
'required',
Rule::notIn(['sprinkles','cherries']),
],
//当使用regex时,必须使用数组,而不是|分隔符,特别是正则中有|时
'email2' => 'unique:users,email_address',
'email3' => 'unique:connection.users,email_address', //指定数据库
'email4' => Rule::unique('users')->where(function ($query){
$query->where('account_id',1);
}),
'custom' => [
'person.*.email' => [
'unique' => 'each person must have a unique e-mail address',
]
],
];
特殊例子
//验证时忽视id
Validator::make($data,[
'email' => [
'required',
Rule::unique('users')->ignore($user->id,'user_id'),
]
]); //在某些情况下,只有在该字段存在于输入数组中时,才可以对字段执行验证检查
$v = Validator::make($data,[
'email' => 'sometimes|required|email',//email只有在data数组中时才会被验证
]); $z = Validator::make($data,[
'email' => 'required|email',
'games' => 'required|numeric',
]);
$z->sometimes('reason','required|max:500',function ($input){
return $input->games >= 100; //当值超过100时,reson才必填
});
$z->sometimes(['reson','cost'],'required',function ($input){
return $input->games >= 100;
});
$validator = Validator::make($request->all(),[
'photos.profile' => 'required|image',//验证数组中的某个key的值
]); $validator = Validator::make($request->all(),[
'person.*.email' => 'email|unique:users',
'person.*.first_name' => 'required_with:person.*.last_name',
]);//验证指定数组输入字段中的每一个email都是唯一的 $request->validate([
'name' => ['required', new Uppercase()],
]);
$validator = Validator::make($this->request,[
'title' => 'required|unique:posts|max:255',
'body' => 'required',
])->validate(); $validator->after(function ($validator){
if ($this->somethingElseIsInvalid()) {
$validator->errors()->add('field', 'Something is wrong with this field!');
}
}); if ($validator->fails()){ } $errors = $validator->errors();
echo $errors->first('email'); //以数组形式获取指定字段的所有错误消息
foreach ($errors->get('email') as $message){ } //验证表单的数组字段,获取数组元素的所欲错误消息
foreach ($errors->get('attachments.*') as $message){ } //查看所有字段的错误消息
foreach ($errors->all() as $message){ } // 检测一个字段是否有错误消息
if ($errors->has('email')){ }
laravel 数据验证规则的更多相关文章
- laravel 数据验证
laravel 数据验证 在保存数据之前进行数据验证 类需要继承 Controller 然后用 $this->validate( $request , ['title' => 'requ ...
- laravel 可用验证规则
accepted 验证的字段必须为 yes. on. 1.或 true.这在确认服务条款是否同意时相当有用. active_url 相当于使用了 PHP 函数 dns_get_record,验证的字段 ...
- yii2之数据验证
一.场景 什么情况下需要使用场景呢?当一个模型需要在不同情境中使用时,若不同情境下需要的数据表字段和数据验证规则有所 不同,则需要定义多个场景来区分不同使用情境.例如,用户注册的时候需要填写email ...
- 05:ModelForm 数据验证 & 生成html & 数据库操作
目录:Django其他篇 01:Django基础篇 02:Django进阶篇 03:Django数据库操作--->Model 04: Form 验证用户数据 & 生成html 05:Mo ...
- Laravel的unique和exists验证规则的优化
本文是Laravel实战:任务管理系统(一)的扩展阅读 原文链接:http://pilishen.com/posts/Improvements-to-the-Laravel-unique-and-ex ...
- MVC数据验证原理及自定义ModelValidatorProvider实现无编译修改验证规则和错误信息
Asp.net MVC中的提供非常简单易用的数据验证解决方案. 通过System.ComponentModel.DataAnnotations提供的很多的验证规则(Required, StringLe ...
- JEECG 不同(角色的)人对同样的字段数据,使用不同的字段验证规则
JEECG智能开发平台v3 开发指南http://www.jeecg.org/book/jeecg_v3.html jeecg: JEECG是一款基于代码生成器的J2EE快速开发平台,开源界“小普元” ...
- Laravel框架 -- Validator 可用的验证规则
accepted 字段值为 yes, on, 或是 1 时,验证才会通过.这在确认"服务条款"是否同意时很有用. active_url 字段值通过 PHP 函数 checkdnsr ...
- laravel验证规则
就拿laravel的登入验证来举例: 1.进入login控制器, use AuthenticatesUsers;从这里点进去找到验证规则 //验证protected function validate ...
随机推荐
- 跨域 CORS 详解 (转)
CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing). 它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从 ...
- UIView属性的讲解
1.父控件和子控件的理解在storyboard中只有UIView是可以在里面拖入子控件的,其他控件不可以(必须通过代码添加)拖入一个UIView控件,在里面添加一些子控件(UIView控件是控制器的V ...
- 500行代码了解Mecached缓存客户端驱动原理
原创不易,求分享.求一键三连 缓存一般是用来加速数据访问的效率,在获取数据耗时高的场景下使用缓存可以有效的提高数据获取的效率. 比如,先从memcached中获取数据,如果没有则查询mysql中的数据 ...
- spring5+Struts2+hibernate5
一,加jar包 加入顺序,个人推荐,spring->struts2->hibernate spring的jar包:基本包共21个+用到的aspectj注解的包2个+日志包1个 ------ ...
- Spring IOC-基于XML配置的容器
Spring IOC-基于XML配置的容器 我们先分析一下AbstractXmlApplicationContext这个容器的加载过程. AbstractXmlApplicationContext的老 ...
- 模块random+os+sys+json+subprocess
模块random+os+sys+json+subprocess 1. random 模块 (产生一个随机值) import random 1 # 随机小数 2 print(random.rando ...
- Solution Set -「LOCAL」冲刺省选 Round XXII
\(\mathscr{Summary}\) 和出题人很有缘分但是没有珍惜.jpg A 题有一个显然的二维偏序斜率式,以及显然的 CDQ 套李超树 \(\mathcal O(n\log^2n)\ ...
- NeurIPS 2017 | QSGD: Communication-Efficient SGD via Gradient Quantization and Encoding
由于良好的可扩展性,随机梯度下降(SGD)的并行实现是最近研究的热点.实现并行化SGD的关键障碍就是节点间梯度更新时的高带宽开销.因此,研究者们提出了一些启发式的梯度压缩方法,使得节点间只传输压缩后的 ...
- Anchor-free目标检测综述 -- Dense Prediction篇
早期目标检测研究以anchor-based为主,设定初始anchor,预测anchor的修正值,分为two-stage目标检测与one-stage目标检测,分别以Faster R-CNN和SSD作 ...
- 思迈特软件Smartbi:专注BI,把产品打造到极致
在企业服务领域,现在的BI(商业智能)无疑是妥妥的风口.不过在20多年前,BI却完全是一幅门庭冷清宾客稀的光景--小型企业高攀不起,大型企业爱答不理. 一些管理者们理所当然地认为,商业是人脑的高阶竞争 ...