我們可以利用Form Request來(lái)封裝表單驗(yàn)證代碼,從而精簡(jiǎn)Controller中的代碼邏輯,使其專注于業(yè)務(wù)。而獨(dú)立出去的表單驗(yàn)證邏輯可以復(fù)用到其它請(qǐng)求中,看過(guò)幾篇文章,大多都是講怎么創(chuàng)建Request,表面看起來(lái)是將邏輯與業(yè)務(wù)分離了,但是沒(méi)有做到復(fù)用,一個(gè)業(yè)務(wù)就得新建一個(gè)Request類實(shí)在太累,索性這里我將項(xiàng)目全部的表單驗(yàn)證放在一個(gè)Request類里,實(shí)現(xiàn)高度可復(fù)用,下面是具體實(shí)現(xiàn)。 首先創(chuàng)建Requestphp artisan make:request CreateUserRequest CreateUserRequest代碼塊?1 <?php 2 3 namespace App\Http\Requests; 4 5 use App\Http\Requests\Request; 6 7 class CreateUserRequest extends Request 8 { 9 //驗(yàn)證規(guī)則可自己添加需要驗(yàn)證的字段 10 protected $rules = [ 11 'Student.userName' => 'required|between:2,4', 12 'Student.userAge' => 'required|integer', 13 'Student.userSex' => 'required|integer', 14 'Student.addr' => 'required', 15 ]; 16 //這里我只寫(xiě)了部分字段,可以定義全部字段 17 protected $strings_key = [ 18 'Student.userName' => '用戶名', 19 'Student.userAge' => '年齡', 20 'Student.userSex' => '性別', 21 'Student.addr' => '地址', 22 ]; 23 //這里我只寫(xiě)了部分情況,可以按需定義 24 protected $strings_val = [ 25 'required'=> '為必填項(xiàng)', 26 'min'=> '最小為:min', 27 'max'=> '最大為:max', 28 'between'=> '長(zhǎng)度在:min和:max之間', 29 'integer'=> '必須為整數(shù)', 30 'sometimes'=> '', 31 ]; 32 33 /** 34 * Determine if the user is authorized to make this request. 35 * 36 * @return bool 37 */ 38 public function authorize() 39 { 40 return true;//修改為true 41 } 42 43 /** 44 * Get the validation rules that apply to the request. 45 * 46 * @return array 47 */ 48 public function rules() 49 { 50 51 $rules = $this->rules; 52 // 根據(jù)不同的情況, 添加不同的驗(yàn)證規(guī)則 53 if (Request::getPathInfo() == '/save')//如果是save方法 54 { 55 $rules['Student.addr'] = 'sometimes'; 56 } 57 if (Request::getPathInfo() == '/edit')//如果是edit方法 58 { 59 $rules['Student.addr'] = 'required|min:5'; 60 } 61 return $rules; 62 63 } 64 //返回給前臺(tái)的錯(cuò)誤信息 65 public function messages(){ 66 $rules = $this->rules(); 67 $k_array = $this->strings_key; 68 $v_array = $this->strings_val; 69 foreach ($rules as $key => $value) { 70 $new_arr = explode('|', $value);//分割成數(shù)組 71 foreach ($new_arr as $k => $v) { 72 $head = strstr($v,':',true);//截取:之前的字符串 73 if ($head) {$v = $head;} 74 $array[$key.'.'.$v] = $k_array[$key].$v_array[$v]; 75 } 76 } 77 return $array; 78 } 79 } 控制器具體方法1 /** 2 * Show the form for creating a new resource. 3 * 4 * @return \Illuminate\Http\Response 5 */ 6 public function save(\App\Http\Requests\CreateUserRequest $request) 7 { 8 //這里會(huì)自動(dòng)調(diào)用表單驗(yàn)證 9 //驗(yàn)證成功后繼續(xù)向下執(zhí)行 10 $data = $request->input('Student'); 11 if(User::create($data)){ 12 return redirect('/')->with('success', '添加成功!'); 13 }else{ 14 return redirect('/create')->with('error', '添加失敗!'); 15 } 16 } 對(duì)應(yīng)的模板文件1 <form class="form-horizontal" method="post" action="save"> 2 <div class="form-group"> 3 <label for="name" class="col-sm-2 control-label">姓名</label> 4 {!! csrf_field() !!} 5 <div class="col-sm-5"> 6 <input type="text" class="form-control" id="name" name="Student[userName]" placeholder="請(qǐng)輸入學(xué)生姓名" value="{{ old('Student')['userName']}}"> 7 </div> 8 <div class="col-sm-5"> 9 <p class="form-control-static text-danger">{{ $errors->first('Student.userName') }}</p> 10 </div> 11 </div> 12 <div class="form-group"> 13 <label for="age" class="col-sm-2 control-label">年齡</label> 14 15 <div class="col-sm-5"> 16 <input type="text" class="form-control" id="age" name="Student[userAge]" placeholder="請(qǐng)輸入學(xué)生年齡" value="{{ old('Student')['userAge']}}"> 17 </div> 18 <div class="col-sm-5"> 19 <p class="form-control-static text-danger">{{$errors->first('Student.userAge')}}</p> 20 </div> 21 </div> 22 <div class="form-group"> 23 <label for="age" class="col-sm-2 control-label">地址</label> 24 25 <div class="col-sm-5"> 26 <input type="text" class="form-control" id="addr" name="Student[addr]" placeholder="請(qǐng)輸?shù)刂? > 27 </div> 28 <div class="col-sm-5"> 29 <p class="form-control-static text-danger">{{$errors->first('Student.addr')}}</p> 30 </div> 31 </div> 32 <div class="form-group"> 33 <label class="col-sm-2 control-label">性別</label> 34 35 <div class="col-sm-5"> 36 <label class="radio-inline"> 37 <input type="radio" name="Student[userSex]" value="1" > 未知 38 </label> 39 <label class="radio-inline"> 40 <input type="radio" name="Student[userSex]" value="2"> 男 41 </label> 42  43 44 <label class="radio-inline"> 45 <input type="radio" name="Student[userSex]" value="3"> 女 46 </label> 47 </div> 48 <div class="col-sm-5"> 49 <p class="form-control-static text-danger">{{ $errors->first('Student.userSex') }}</p> 50 </div> 51 </div> 52 <div class="form-group"> 53 <div class="col-sm-offset-2 col-sm-10"> 54 <button type="submit" class="btn btn-primary">提交</button> 55 </div> 56 </div> 57 </form> 效果展示? ? ? ?寫(xiě)在最后通過(guò)文本可以看到, Form Requests 對(duì)于簡(jiǎn)化表單請(qǐng)求的數(shù)據(jù)校驗(yàn)是非常強(qiáng)大和方便的.這里我做了一些修改,使得rules()能夠可復(fù)用且只新增一個(gè)Request。如果有更好的解決方法,歡迎留言。轉(zhuǎn)載:https://www.jianshu.com/p/0225e63454e8 ? 來(lái)源:https://www./content-4-433301.html |
|
來(lái)自: 印度阿三17 > 《開(kāi)發(fā)》