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')){ }
?
相關參考:php中文網
? 版權聲明
文章版權歸作者所有,未經允許請勿轉載。
THE END
喜歡就支持一下吧
相關推薦