laravel表單驗證常用的驗證規則


首先創建Request:項目中我是在app->http->Request->自己創立一個文件夾(我的是User)->驗證文件(CreateUserValidate)  這個文件夾建立的驗證

CreateUserValidate代碼塊

 1 <?php
 2 
 3 namespace App\Http\Requests\User;
 4 
 5 use App\Http\Requests\BaseValidate;
 6 
 7 class CreateUserValidate extends BaseValidate
 8 {
 9     /**
10      * Determine if the user is authorized to make this request.
11      *
12      * @return bool
13      */
14     public function authorize()
15     {
16         return true;
17     }
18 
19     /**
20      * Get the validation rules that apply to the request.
21      *
22      * @return array
23      */
24     public function rules()
25     {
26         if($this->isMethod('post')){
27             return [
28                 'score' => 'required|min:6|max:6',
29                 'userName' => 'required|between:2,4',
30                 'userAge' => 'required|integer',
31                 'addr' => 'required',
32             ];
33         }
34         return [];
35     }
36 
37     public function messages()
38     {
39         return [
40             'score.required' => '分數必填',
41             'score.min' => '分數必需大於5位',
42             'score.max' => '分數必需小於7位',
43             'userName.required' => '用戶名必填',
44             'userName.between' => '用戶名長度必須在2:4之間',
45             'userAge.required' => '年齡必填',
46             'userAge.integer' => '年齡必需是整數',
47             'addr.required'=>'地址必填'
48         ];
49     }
50 }
51       

控制器具體方法

public function add(CreateUserValidate$request,$id){
       
}

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM