免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
自定義laravel表單請求驗(yàn)證類(FormRequest共用一個(gè)rules())

我們可以利用Form Request來封裝表單驗(yàn)證代碼,從而精簡Controller中的代碼邏輯,使其專注于業(yè)務(wù)。而獨(dú)立出去的表單驗(yàn)證邏輯可以復(fù)用到其它請求中,看過幾篇文章,大多都是講怎么創(chuàng)建Request,表面看起來是將邏輯與業(yè)務(wù)分離了,但是沒有做到復(fù)用,一個(gè)業(yè)務(wù)就得新建一個(gè)Request類實(shí)在太累,索性這里我將項(xiàng)目全部的表單驗(yàn)證放在一個(gè)Request類里,實(shí)現(xiàn)高度可復(fù)用,下面是具體實(shí)現(xiàn)。

首先創(chuàng)建Request

php 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     //這里我只寫了部分字段,可以定義全部字段17     protected $strings_key = [18         'Student.userName' => '用戶名',19         'Student.userAge' => '年齡',20         'Student.userSex' => '性別',21         'Student.addr' => '地址',22     ];23     //這里我只寫了部分情況,可以按需定義24     protected $strings_val = [25         'required'=> '為必填項(xiàng)',26         'min'=> '最小為:min',27         'max'=> '最大為:max',28         'between'=> '長度在:min和:max之間',29         'integer'=> '必須為整數(shù)',30         'sometimes'=> '',31     ];32 33     /**34      * Determine if the user is authorized to make this request.35      *36      * @return bool37      */38     public function authorize()39     {40         return true;//修改為true41     }42 43     /**44      * Get the validation rules that apply to the request.45      *46      * @return array47      */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   //返回給前臺的錯(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     }

對應(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="請輸入學(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="請輸入學(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="請輸?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 ![QQ截圖20170613152555.png](http://upload-images.jianshu.io/upload_images/2825702-f008b65789a425f4.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240)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>

效果展示

?

?

?

?

寫在最后


通過文本可以看到, Form Requests 對于簡化表單請求的數(shù)據(jù)校驗(yàn)是非常強(qiáng)大和方便的.這里我做了一些修改,使得rules()能夠可復(fù)用且只新增一個(gè)Request。如果有更好的解決方法,歡迎留言。

轉(zhuǎn)載:https://www.jianshu.com/p/0225e63454e8

?

來源:https://www.icode9.com/content-4-433301.html
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
ASP.NET常用的6大對象
HTTP協(xié)議與HTML表單(再談GET與POST的區(qū)別)
存儲(chǔ)過程基本目標(biāo)
Django實(shí)戰(zhàn)
Web-第八天 Servlet學(xué)習(xí)【悟空教程】
真不是吹,Spring 里這款牛逼的網(wǎng)絡(luò)工具庫你可能沒用過!
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服