• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    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 验证码认证学习记录小结
    • Laravel实现登录跳转功能
    • laravel 解决强制跳转 https的问题
    • Laravel重定向,a链接跳转,控制器跳转示例
    • 解决Laravel使用验证时跳转到首页的问题
    上一篇:php适配器模式简单应用示例
    下一篇:php装饰者模式简单应用案例分析
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    laravel 数据验证规则详解 laravel,数据,验证,规则,详解,