laravel怎樣驗證數(shù)據(jù)規(guī)則-創(chuàng)新互聯(lián)

這篇文章主要介紹laravel怎樣驗證數(shù)據(jù)規(guī)則,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

成都創(chuàng)新互聯(lián)公司堅信:善待客戶,將會成為終身客戶。我們能堅持多年,是因為我們一直可值得信賴。我們從不忽悠初訪客戶,我們用心做好本職工作,不忘初心,方得始終。10多年網(wǎng)站建設(shè)經(jīng)驗成都創(chuàng)新互聯(lián)公司是成都老牌網(wǎng)站營銷服務(wù)商,為您提供做網(wǎng)站、成都做網(wǎng)站、網(wǎng)站設(shè)計、H5場景定制、網(wǎng)站制作、品牌網(wǎng)站建設(shè)、小程序定制開發(fā)服務(wù),給眾多知名企業(yè)提供過好品質(zhì)的建站服務(wù)。

laravel 怎樣驗證數(shù)據(jù)規(guī)則


return ['accepted'                               => '必須為yes,on,1,true',
        'active_url'                             => '是否是一個合法的url,基于PHP的checkdnsrr函數(shù),因此也可以用來驗證郵箱地址是否存在',
        'after:date'                             => '驗證字段必須是給定日期后的值,比如required|date|after:tomorrow,通過PHP函數(shù)strtotime來驗證',
        'after_or_equal:date'                    => '大于等于',
        'alpha'                                  => '驗證字段必須全是字母',
        'alpha_dash'                             => '驗證字段可能具有字母、數(shù)字、破折號、下劃線',
        'alpha_num'                              => '驗證字段必須全是字母和數(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字段值一致,比如,要驗證的是password,輸入中必須存在匹配的password_confirmation字段',
        'date'                                   => '通過strtotime校驗的有效日期', 'date_equals:date' => '等于',
        'date_format:format'                     => 'date和date_format不應(yīng)該同時使用,按指定時間格式傳值',
        'different:field'                        => '驗證的字段值必須與字段field的值相同', 'digits:value' => '必須是數(shù)字,并且有確切的值',
        'digits_between:min,max'                 => '字段長度必須在min,max之間',
        'dimensions'                             => '驗證的文件是圖片并且圖片比例必須符合規(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' => '驗證的字段存在時不能為空',
        'image'                                  => '驗證的文件必須是圖像,jpeg,png,bmp,gif,svg', 'in:foo,bar,...' => '驗證的字段必須包含在給定的值列表中',
        'in_array:anotherfield'                  => '驗證的字段必須存在于另一個字段的值中',
        'integer'                                => '整數(shù)',
        'ip'                                     => 'ip地址', 'ipv4' => 'ipv4地址',
        'ipv6'                                   => 'ipv6地址',
        'json'                                   => 'json字符串',
        'max:value'                              => '大于',
        'mimetypes:text/plain,...'               => '驗證的文件必須與給定的MIME類型匹配',
        'mimes:foo,bar,...'                      => '驗證的文件必須具有列出的其中一個擴(kuò)展名對應(yīng)的MIME類型',
        'min:value'                              => '小于',
        'nullable'                               => '可為null,可以包含空值的字符串和整數(shù)',
        'not_in:foo,bar...'                      => '不包含',
        'numeric'                                => '必須為數(shù)字',
        'present'                                => '驗證的字段必須存在于輸入數(shù)據(jù)中,但可以為空',
        'regex:pattern'                          => '驗證的字段必須與給定正則表達(dá)式匹配',
        'required'                               => '驗證的字段必須存在于輸入數(shù)據(jù)中,但不可以為空',
    //以下情況視為空:1.該值為null,2.空字符串,3.空數(shù)組或空的可數(shù)對象,4.沒有路徑的上傳文件
        'required_if:anotherfield,value,...'     => '如果指定的anotherfield等于value時,被驗證的字段必須存在且不為空',
        'required_unless:anotherfield,value,...' => '如果指定的anotherfield等于value時,被驗證的字段不必存在',
        'required_with:foo,bar,...'              => '只要指定的其它字段中有任意一個字段存在,被驗證的字段就必須存在且不為空',
        'required_with_all:foo,bar,...'          => '當(dāng)指定的其它字段必須全部存在時,被驗證的字段才必須存在且不為空',
        'required_without_all:foo,bar,...'       => '當(dāng)指定的其它字段必須全部不存在時,被驗證的字段必須存在且不為空',
        'required_without:foo,bar,...'           => '當(dāng)指定的其它字段有一個字段不存在,被驗證的字段就必須存在且不為空',
        'same:field'                             => '給定字段必須與驗證字段匹配', 'size:value' => '驗證字段必須具有與給定值匹配的大小,對字符串,value對應(yīng)字符數(shù);對數(shù)字,對應(yīng)給定的整數(shù)值;對數(shù)組,對應(yīng)count值;對文件,是文件大?。╧b)',
        'timezone'                               => '驗證字段是有效的時區(qū)標(biāo)識符,根據(jù)PHP函數(shù)timezone_identifiers_list判斷', 'unique:table,column,except,idColumn' => '驗證字段必須是數(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', //驗證文件擴(kuò)展名,規(guī)則上也會驗證文件的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',
        ]
    ],];

特殊例子

//驗證時忽視idValidator::make($data,[    'email' => [        'required',
        Rule::unique('users')->ignore($user->id,'user_id'),
    ]
]);//在某些情況下,只有在該字段存在于輸入數(shù)組中時,才可以對字段執(zhí)行驗證檢查$v = Validator::make($data,[    'email' => 'sometimes|required|email',//email只有在data數(shù)組中時才會被驗證]);$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',//驗證數(shù)組中的某個key的值]);$validator = Validator::make($request->all(),[    'person.*.email' => 'email|unique:users',    'person.*.first_name' => 'required_with:person.*.last_name',]);//驗證指定數(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){
}//驗證表單的數(shù)組字段,獲取數(shù)組元素的所欲錯誤消息foreach ($errors->get('attachments.*') as $message){
}//查看所有字段的錯誤消息foreach ($errors->all() as $message){
}// 檢測一個字段是否有錯誤消息if ($errors->has('email')){
}

以上是“l(fā)aravel怎樣驗證數(shù)據(jù)規(guī)則”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)頁名稱:laravel怎樣驗證數(shù)據(jù)規(guī)則-創(chuàng)新互聯(lián)
路徑分享:http://muchs.cn/article46/djggeg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、微信公眾號App開發(fā)、關(guān)鍵詞優(yōu)化、靜態(tài)網(wǎng)站虛擬主機(jī)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)