乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

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

       印度阿三17 2019-09-01

      我們可以利用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)建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     //這里我只寫(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 ![QQ截圖20170613152555.png](http://upload-images./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>

      效果展示

      ?

      ?

      ?

      ?

      寫(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

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多