2016-08-22 134 views
0

我使用laravel 5.2 ..當提交表單並檢查數據驗證存儲函數並將錯誤數據發回給視圖時,錯誤不會顯示。 這裏是代碼userController.php驗證錯誤消息不會顯示在laravel 5.2中查看

<?php 

namespace App\Http\Controllers; 
use Illuminate\Support\Facades\Input; 
use Illuminate\Http\Request; 
use App\user; 
use Hash; 
use App\Http\Requests; 
use Validator; 
use Illuminate\Support\Facades\Redirect; 

class userController extends Controller 
{ 
    /** 
    * Display a listing of the resource. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function index() 
    { 
     // 
    } 

    /** 
    * Show the form for creating a new resource. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function create() 
    { 
     return view('user.usr_signup'); 
    } 
    /** 
    * Store a newly created resource in storage. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @return \Illuminate\Http\Response 
    */ 
    public function store(Request $request) 
    { 
     $firstName=input::get('firstName'); 
     $lastName=input::get('firstName'); 
     $password=Hash::make(input::get('pass')); 
     $gender=input::get('gender'); 
     $mail=input::get('mail'); 
     $newUser= new user; 
     $newUser->firstName=$firstName; 
     $newUser->lastName=$lastName; 
     $newUser->password=$password; 
     $newUser->mail=$mail; 
     $newUser->gender=$gender; 

     $validator = Validator::make(Input::all(), [ 
     'firstName' => 'required', 
     'lastName' => 'required', 
     'mail'=>'required|unique:user', 
     'pass'=>'required' 
     ]); 

     if ($validator->fails()) { 
      return Redirect::back()->withInput()->withErrors($validator->messages()) ; 
     } 
     else{ 
       $newUser->save(); 

     return redirect('[email protected]'); 
     } 
    } 
    /** 
    * Display the specified resource. 
    * 
    * @param int $id 
    * @return \Illuminate\Http\Response 
    */ 
    public function show($id) 
    { 
     // 
    } 

    /** 
    * Show the form for editing the specified resource. 
    * 
    * @param int $id 
    * @return \Illuminate\Http\Response 
    */ 
    public function edit($id) 
    { 
     // 
    } 

    /** 
    * Update the specified resource in storage. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param int $id 
    * @return \Illuminate\Http\Response 
    */ 
    public function update(Request $request, $id) 
    { 
     // 
    } 

    /** 
    * Remove the specified resource from storage. 
    * 
    * @param int $id 
    * @return \Illuminate\Http\Response 
    */ 
    public function destroy($id) 
    { 
     // 
    } 
} 

,並在視圖user.usr_signup.blade.php這裏的代碼從驗證返回的錯誤信息

<form method="post" id="signUpForm" action="{{action('[email protected]')}}"> 
      <input type="hidden" name="_token" value="{{ csrf_token() }}"> 
      <div class="row"> 
      <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> 
       <div class="form-group"> 
       <label for="firstName" class="sr-only">First Name</label> 
       <input type="text" class="form-control input-lg" id="firstName" name="firstName" placeholder="firstName"> 
       {{$errors->first('firstName')}} 
       </div> 
      </div> 
      <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6"> 
       <div class="form-group"> 
       <label for="lastName" class="sr-only">Last Name</label> 
       <input type="text" class="form-control input-lg" id="lastName" name="lastName" placeholder="lastName"> 
       </div> 
      </div> 
      </div> 
      <div class="form-group"> 
      <label class="sr-only" for="user">email address</label> 
      <input type="text" class="form-control input-lg" id="user" name="mail" placeholder="email address"> 
      </div> 
      <div class="form-group"> 
      <label for="gender" class="sr-only">Gender</label> 
      <select class="form-control input-lg input-block" id="gender" name="gender" size="1"> 
       <option value="1">male</option> 
       <option value="2">female</option> 
      </select> 
      </div> 
      <div class="form-group"> 
      <label for="password" class="sr-only">Password</label> 
      <input type="password" class="form-control input-lg input-block" id="pass" name="pass" placeholder="password"> 
      </div> 
      <div class="form-group"> 
      <label for="password" class="sr-only">Reenter Password</label> 
      <input type="password" class="form-control input-lg input-block" id="reEnteredPass" name="rePass" placeholder="reenter password"> 
      </div> 
      <div class="form-group"> 
      <button type="submit" class="btn btn-success btn-lg btn-block" id="submitSignUpForm" name="submitSignUpFormButton">submit </button> 
      </div> 

     </form> 
     @if (count($errors->all()) > 0) 
      <div class="alert alert-danger"> 
      <ul> 
       @foreach ($errors->all() as $error) 
       <li>{{ $error }}</li> 
       @endforeach 
      </ul> 
      </div> 
      @else 
      @endif 

怎麼能我修復它?

回答

0

變化return Redirect::back()->withInput()->withErrors($validator->messages()) ;return Redirect::back()->withInput()->withErrors($validator) ;

參考文檔here

+0

還是不行! – ako

+0

你在routes.php中使用'web'中間件嗎? – kkyeboah

相關問題