laravel 數(shù)據(jù)驗(yàn)證規(guī)則詳解
更新時間:2019年10月23日 10:17:22 作者:侯蜀黍
今天小編就為大家分享一篇laravel 數(shù)據(jù)驗(yàn)證規(guī)則詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
如下所示:
return [ 'accepted' => '必須為yes,on,1,true', 'active_url' => '是否是一個合法的url,基于PHP的checkdnsrr函數(shù),因此也可以用來驗(yàn)證郵箱地址是否存在', 'after:date' => '驗(yàn)證字段必須是給定日期后的值,比如required|date|after:tomorrow,通過PHP函數(shù)strtotime來驗(yàn)證', 'after_or_equal:date' => '大于等于', 'alpha' => '驗(yàn)證字段必須全是字母', 'alpha_dash' => '驗(yàn)證字段可能具有字母、數(shù)字、破折號、下劃線', 'alpha_num' => '驗(yàn)證字段必須全是字母和數(shù)字', 'array' => '數(shù)組', 'before:date' => '小于', 'before_or_equal:date' => '小于等于', 'between:min,max' => '給定大小在min,max之間,字符串,數(shù)字,數(shù)組或者文件大小都用size函數(shù)評估', 'boolean' => '必須為能轉(zhuǎn)化為布爾值的參數(shù),比如:true,false,1,0,"1","0"', 'confirmed' => '字段必須與foo_confirmation字段值一致,比如,要驗(yàn)證的是password,輸入中必須存在匹配的password_confirmation字段', 'date' => '通過strtotime校驗(yàn)的有效日期', 'date_equals:date' => '等于', 'date_format:format' => 'date和date_format不應(yīng)該同時使用,按指定時間格式傳值', 'different:field' => '驗(yàn)證的字段值必須與字段field的值相同', 'digits:value' => '必須是數(shù)字,并且有確切的值', 'digits_between:min,max' => '字段長度必須在min,max之間', 'dimensions' => '驗(yàn)證的文件是圖片并且圖片比例必須符合規(guī)則,比如dimensions:min_width=100,min_height=200,可用 的規(guī)則有min_width,max_width,min_height,max_height,width,height,ratio', 'distinct' => '無重復(fù)值', 'email' => '符合e-mail地址格式', 'exists:table,column' => '必須存在于指定的數(shù)據(jù)庫表中', 'file' => '成功上傳的文件', 'filled' => '驗(yàn)證的字段存在時不能為空', 'image' => '驗(yàn)證的文件必須是圖像,jpeg,png,bmp,gif,svg', 'in:foo,bar,...' => '驗(yàn)證的字段必須包含在給定的值列表中', 'in_array:anotherfield' => '驗(yàn)證的字段必須存在于另一個字段的值中', 'integer' => '整數(shù)', 'ip' => 'ip地址', 'ipv4' => 'ipv4地址', 'ipv6' => 'ipv6地址', 'json' => 'json字符串', 'max:value' => '大于', 'mimetypes:text/plain,...' => '驗(yàn)證的文件必須與給定的MIME類型匹配', 'mimes:foo,bar,...' => '驗(yàn)證的文件必須具有列出的其中一個擴(kuò)展名對應(yīng)的MIME類型', 'min:value' => '小于', 'nullable' => '可為null,可以包含空值的字符串和整數(shù)', 'not_in:foo,bar...' => '不包含', 'numeric' => '必須為數(shù)字', 'present' => '驗(yàn)證的字段必須存在于輸入數(shù)據(jù)中,但可以為空', 'regex:pattern' => '驗(yàn)證的字段必須與給定正則表達(dá)式匹配', 'required' => '驗(yàn)證的字段必須存在于輸入數(shù)據(jù)中,但不可以為空', //以下情況視為空:1.該值為null,2.空字符串,3.空數(shù)組或空的可數(shù)對象,4.沒有路徑的上傳文件 'required_if:anotherfield,value,...' => '如果指定的anotherfield等于value時,被驗(yàn)證的字段必須存在且不為空', 'required_unless:anotherfield,value,...' => '如果指定的anotherfield等于value時,被驗(yàn)證的字段不必存在', 'required_with:foo,bar,...' => '只要指定的其它字段中有任意一個字段存在,被驗(yàn)證的字段就必須存在且不為空', 'required_with_all:foo,bar,...' => '當(dāng)指定的其它字段必須全部存在時,被驗(yàn)證的字段才必須存在且不為空', 'required_without_all:foo,bar,...' => '當(dāng)指定的其它字段必須全部不存在時,被驗(yàn)證的字段必須存在且不為空', 'required_without:foo,bar,...' => '當(dāng)指定的其它字段有一個字段不存在,被驗(yàn)證的字段就必須存在且不為空', 'same:field' => '給定字段必須與驗(yàn)證字段匹配', 'size:value' => '驗(yàn)證字段必須具有與給定值匹配的大小,對字符串,value對應(yīng)字符數(shù);對數(shù)字,對應(yīng)給定的 整數(shù)值;對數(shù)組,對應(yīng)count值;對文件,是文件大?。╧b)', 'timezone' => '驗(yàn)證字段是有效的時區(qū)標(biāo)識符,根據(jù)PHP函數(shù)timezone_identifiers_list判斷', 'unique:table,column,except,idColumn' => '驗(yàn)證字段必須是數(shù)據(jù)庫中唯一的', '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', //不允許重復(fù) 'state' =>'exists:states', //指定表 'state1' => 'exists:states,abbreviation', //指定表和字段 'email' => 'exists:connection.staff,email', //指定查詢的數(shù)據(jù)庫 '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', //驗(yàn)證文件擴(kuò)展名,規(guī)則上也會驗(yàn)證文件的MIME類型,通過讀取文件的內(nèi)容以猜測它的MIME類型 'toppings' => [ 'required', Rule::notIn(['sprinkles','cherries']), ], //當(dāng)使用regex時,必須使用數(shù)組,而不是|分隔符,特別是正則中有|時 'email2' => 'unique:users,email_address', 'email3' => 'unique:connection.users,email_address', //指定數(shù)據(jù)庫 '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', ] ], ];
特殊例子
//驗(yàn)證時忽視id Validator::make($data,[ 'email' => [ 'required', Rule::unique('users')->ignore($user->id,'user_id'), ] ]); //在某些情況下,只有在該字段存在于輸入數(shù)組中時,才可以對字段執(zhí)行驗(yàn)證檢查 $v = Validator::make($data,[ 'email' => 'sometimes|required|email',//email只有在data數(shù)組中時才會被驗(yàn)證 ]); $z = Validator::make($data,[ 'email' => 'required|email', 'games' => 'required|numeric', ]); $z->sometimes('reason','required|max:500',function ($input){ return $input->games >= 100; //當(dāng)值超過100時,reson才必填 }); $z->sometimes(['reson','cost'],'required',function ($input){ return $input->games >= 100; }); $validator = Validator::make($request->all(),[ 'photos.profile' => 'required|image',//驗(yàn)證數(shù)組中的某個key的值 ]); $validator = Validator::make($request->all(),[ 'person.*.email' => 'email|unique:users', 'person.*.first_name' => 'required_with:person.*.last_name', ]);//驗(yàn)證指定數(shù)組輸入字段中的每一個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'); //以數(shù)組形式獲取指定字段的所有錯誤消息 foreach ($errors->get('email') as $message){ } //驗(yàn)證表單的數(shù)組字段,獲取數(shù)組元素的所欲錯誤消息 foreach ($errors->get('attachments.*') as $message){ } //查看所有字段的錯誤消息 foreach ($errors->all() as $message){ } // 檢測一個字段是否有錯誤消息 if ($errors->has('email')){ }
以上這篇laravel 數(shù)據(jù)驗(yàn)證規(guī)則詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
ThinkPHP3.2.3實(shí)現(xiàn)分頁的方法詳解
這篇文章主要介紹了ThinkPHP3.2.3實(shí)現(xiàn)分頁的方法,詳細(xì)分析了thinkPHP3.2.3分頁操作的相關(guān)技巧,包括分頁類的引用、數(shù)據(jù)庫查詢操作、分頁參數(shù)設(shè)置及前端視圖界面設(shè)計(jì)等,需要的朋友可以參考下2016-06-06PHP機(jī)器學(xué)習(xí)庫php-ml的簡單測試和使用方法
下面小編就為大家?guī)硪黄狿HP機(jī)器學(xué)習(xí)庫php-ml的簡單測試和使用方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07分享六個比較好用的php數(shù)組Array函數(shù)
這篇文章給大家分享六個比較好用的php數(shù)組Array函數(shù),非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-07-07解決php用mysql方式連接數(shù)據(jù)庫出現(xiàn)Deprecated報(bào)錯問題
這篇文章主要介紹了php用mysql方式連接數(shù)據(jù)庫出現(xiàn)Deprecated報(bào)錯,需要的朋友可以參考下2019-12-12