2017-08-03 165 views
0

我在Laravel 5.4中創建了一個登錄函數,並且當密碼不正確時,我想在視圖中顯示錯誤消息。此外,我還有一個自定義消息用於帳戶審批,因此對我來說有點困難。同時我把這些信息放在一起,但不是很方便用戶。我怎樣才能將它們分開?Laravel「密碼錯誤」錯誤信息

這是我的控制器:

public function login(Request $request) 
{ 
    // validate the form data 
    $this->validate($request, [ 
     'email' => 'required|email|exists:users,email', 
     'password' => 'required|min:6' 
    ]); 

    // attempt to log 
    if (Auth::attempt(['approve' => '1', 'email' => $request->email, 'password' => $request->password ], $request->remember)) { 

     // if successful -> redirect forward 
     return redirect()->intended(route('user.overview')); 
    } 

    // if unsuccessful -> redirect back 
    return redirect()->back()->withInput($request->only('email', 'remember'))->withErrors([ 
     'approve' => 'Wrong password or this account not approved yet.', 
    ]); 
} 

至於結果,我想有兩個單獨的電文代替Wrong password or this account not approved yet

如果密碼是錯誤的顯示:Password is wrong 如果賬戶沒有批准顯示:This account not approved yet

+0

你可以使用這樣的:'\會議::閃存(「錯誤」,「密碼錯誤或此帳戶未獲批准尚未」); return redirect() - > back() - > withInput() - > withErrors($ errors);'然後將會話消息打印在刀片文件中! –

回答

0

你可以通過自定義錯誤消息爲每個驗證規則,你可以這樣做:

public function login(Request $request) 
    { 
     //Error messages 
     $messages = [ 
      "email.required' => "Email is required", 
      "email.email' => "Email is not valid", 
      "email.exists' => "Email doesn't exists", 
      "password.required" => "Password is required", 
      "password.min" => "Password must be at least 6 characters" 
     ]; 

     // validate the form data 
     $validator = Validator::make($request->all(), [ 
       'email' => 'required|email|exists:users,email', 
       'password' => 'required|min:6' 
      ], $messages); 

     if ($validator->fails()) { 
      return back()->withErrors($validator)->withInput(); 
     } else { 
      // attempt to log 
      if (Auth::attempt(['approve' => '1', 'email' => $request->email, 'password' => $request->password ], $request->remember)) { 
       // if successful -> redirect forward 
       return redirect()->intended(route('user.overview')); 
      } 

      // if unsuccessful -> redirect back 
      return redirect()->back()->withInput($request->only('email', 'remember'))->withErrors([ 
       'approve' => 'Wrong password or this account not approved yet.', 
      ]); 
     } 
    } 

在此之前,你必須包括Validator類:

use Illuminate\Support\Facades\Validator; 
+0

嗯......有趣,但我應該在哪裏放置消息'密碼錯誤'? –

+0

當你驗證Auth :: attempt時,如果失敗,你可以添加一條消息來做這件事: $ validator-> getMessageBag() - > add('password','Wrong password'); 並在你的redirect() - > back()上返回withErrors($ validator) –

0

您可以這樣用:

return Redirect::back()->withInput(Input::all()); 

如果您使用Form Request Validation,這正是Laravel如何將錯誤和給定輸入重定向到您的方式。

\Illuminate\Foundation\Validation\ValidatesRequests摘錄:

return redirect()->to($this->getRedirectUrl()) 
        ->withInput($request->input()) 
        ->withErrors($errors, $this->errorBag()); 

控制器:

public function login(Request $request) 
{ 
    // validate the form data 
    $this->validate($request, [ 
     'email' => 'required|email|exists:users,email', 
     'password' => 'required|min:6' 
    ]); 

    // attempt to log 
    if (Auth::attempt(['approve' => '1', 'email' => $request->email, 'password' => $request->password ], $request->remember)) { 

     // if successful -> redirect forward 
     return redirect()->intended(route('user.overview')); 
    } 

    // if unsuccessful -> redirect back 
    return Redirect::back() 
      ->withInput() 
      ->withErrors(
       [ 
        'password' => 'Wrong Password', 
       ], 
       [ 
        'approve' => 'Account not approved', 
       ], 
      ); 
} 
+0

抱歉也許你想明白我的意思,我想在密碼不正確時顯示錯誤 –

+0

嘗試使用'return redirect :: back() - > withInput() - > withFlashMessage('錯誤的密碼或此帳戶尚未獲得批准。') ;' –

+0

我想分開:如果密碼不匹配顯示:錯誤的密碼。如果帳戶未獲批准顯示:帳戶未獲批准。 –