laravel怎样验证数据规则

发布时间:2021-01-25 12:34:49 作者:小新
来源:亿速云 阅读:142

这篇文章主要介绍laravel怎样验证数据规则,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

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',
        ]
    ],];

特殊例子

//验证时忽视idValidator::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怎样验证数据规则”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. layui修改表单验证规则
  2. 如何修改Laravel FormRequest验证实现场景验证

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

laravel

上一篇:Laravel ORM对Model::find方法进行缓存的示例

下一篇:基于Laravel开发会员分销系统的示例

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》