咱們能夠利用Form Request來封裝表單驗證代碼,從而精簡Controller中的代碼邏輯,使其專一於業務。而獨立出去的表單驗證邏輯能夠複用到其它請求中,看過幾篇文章,大多都是講怎麼建立Request,表面看起來是將邏輯與業務分離了,可是沒有作到複用,一個業務就得新建一個Request類實在太累,索性這裏我將項目所有的表單驗證放在一個Request類裏,實現高度可複用,下面是具體實現。php
php artisan make:request CreateUserRequest
<?php namespace App\Http\Requests; use App\Http\Requests\Request; class CreateUserRequest extends Request { //驗證規則可本身添加須要驗證的字段 protected $rules = [ 'Student.userName' => 'required|between:2,4', 'Student.userAge' => 'required|integer', 'Student.userSex' => 'required|integer', 'Student.addr' => 'required', ]; //這裏我只寫了部分字段,能夠定義所有字段 protected $strings_key = [ 'Student.userName' => '用戶名', 'Student.userAge' => '年齡', 'Student.userSex' => '性別', 'Student.addr' => '地址', ]; //這裏我只寫了部分狀況,能夠按需定義 protected $strings_val = [ 'required'=> '爲必填項', 'min'=> '最小爲:min', 'max'=> '最大爲:max', 'between'=> '長度在:min和:max之間', 'integer'=> '必須爲整數', 'sometimes'=> '', ]; /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return true;//修改成true } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { $rules = $this->rules; // 根據不一樣的狀況, 添加不一樣的驗證規則 if (Request::getPathInfo() == '/save')//若是是save方法 { $rules['Student.addr'] = 'sometimes'; } if (Request::getPathInfo() == '/edit')//若是是edit方法 { $rules['Student.addr'] = 'required|min:5'; } return $rules; } //返回給前臺的錯誤信息 public function messages(){ $rules = $this->rules(); $k_array = $this->strings_key; $v_array = $this->strings_val; foreach ($rules as $key => $value) { $new_arr = explode('|', $value);//分割成數組 foreach ($new_arr as $k => $v) { $head = strstr($v,':',true);//截取:以前的字符串 if ($head) {$v = $head;} $array[$key.'.'.$v] = $k_array[$key].$v_array[$v]; } } return $array; } }
/** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function save(\App\Http\Requests\CreateUserRequest $request) { //這裏會自動調用表單驗證 //驗證成功後繼續向下執行 $data = $request->input('Student'); if(User::create($data)){ return redirect('/')->with('success', '添加成功!'); }else{ return redirect('/create')->with('error', '添加失敗!'); } }
<form class="form-horizontal" method="post" action="save"> <div class="form-group"> <label for="name" class="col-sm-2 control-label">姓名</label> {!! csrf_field() !!} <div class="col-sm-5"> <input type="text" class="form-control" id="name" name="Student[userName]" placeholder="請輸入學生姓名" value="{{ old('Student')['userName']}}"> </div> <div class="col-sm-5"> <p class="form-control-static text-danger">{{ $errors->first('Student.userName') }}</p> </div> </div> <div class="form-group"> <label for="age" class="col-sm-2 control-label">年齡</label> <div class="col-sm-5"> <input type="text" class="form-control" id="age" name="Student[userAge]" placeholder="請輸入學生年齡" value="{{ old('Student')['userAge']}}"> </div> <div class="col-sm-5"> <p class="form-control-static text-danger">{{$errors->first('Student.userAge')}}</p> </div> </div> <div class="form-group"> <label for="age" class="col-sm-2 control-label">地址</label> <div class="col-sm-5"> <input type="text" class="form-control" id="addr" name="Student[addr]" placeholder="請輸地址" > </div> <div class="col-sm-5"> <p class="form-control-static text-danger">{{$errors->first('Student.addr')}}</p> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">性別</label> <div class="col-sm-5"> <label class="radio-inline"> <input type="radio" name="Student[userSex]" value="1" > 未知 </label> <label class="radio-inline"> <input type="radio" name="Student[userSex]" value="2"> 男 </label> ![QQ截圖20170613152555.png](http://upload-images.jianshu.io/upload_images/2825702-f008b65789a425f4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) <label class="radio-inline"> <input type="radio" name="Student[userSex]" value="3"> 女 </label> </div> <div class="col-sm-5"> <p class="form-control-static text-danger">{{ $errors->first('Student.userSex') }}</p> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-primary">提交</button> </div> </div> </form>