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