laravel5表單驗證


學習laravel框架有一段時間了,覺得它自帶的表單驗證特別好用,和大家分享分享

對於一些驗證規則手冊上都有,相信大家看了就會,我簡單的說下怎么使用自定義正則驗證:

驗證手機號:'tel' => array('regex:/^1(3|4|5|7|8)\d{9}$/'),  直接加入到validate里驗證即可

 

還有一點就是在驗證時,驗證出錯后怎么保持原來輸入的信息,下面貼上代碼,更容易理解:

1、控制器

 1 //驗證表單
 2     public function postCheck(Request $req)
 3     {    
 4       
 5         $this->validate($req, 
 6          [
 7             'email' => 'bail|required|email|max:25|unique:user,email',
 8             'pwd_confirmation' => 'required|alpha_dash|between:6,20',
 9             'pwd'=>'required|confirmed',
10             'explain'=>'required',
11             'checkbox'=>'accepted',
12         ],
13         [    'email.required' =>'We need to know your e-mail address!',   //自定義錯誤信息
14             'email.email' => 'Please fill in the correct email address.!',
15             'email.max' => 'Mailbox length maximum 25 characters!',
16             'email.unique' => 'The mailbox is too fire, has been registered!',
17 
18             'pwd_confirmation.required' =>'Please enter your password!',
19             'pwd_confirmation.between' => 'Password must be 6 to 20 characters!',
20 
21             'pwd.required' => 'Please Confirm your password!',
22             'pwd.confirmed' => 'Confirm password error!',
23 
24             'explain.required' => 'Please fill in the details!',
25             
26             'checkbox.accepted' => 'Please agree to the registration agreement!',
27 
28         ]);
29               //驗證通過后數據入庫
30     
31         $date = $req->all();  //接到的參數
32         $res = $this->add($date);
33 }
34     

2、視圖(只寫了一個,其他的復制)

 1                 <div class="form-group">
 2                     <input type="email" class="form-control" placeholder="請輸入郵箱" required="" id="email" name="email" value="{{old('email')}}">
 3                 </div>
          //錯誤信息顯示 4 @if($errors->has('email')) 5 <div class="alert alert-danger"> 6 @foreach($errors->get('email') as $error) 7 {{$error}} 8 @endforeach 9 </div> 10 @endif

 


免責聲明!

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



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