2013-08-21 47 views

回答

24
return Redirect::to('admin/users/create') 
     ->withInput() 
     ->withErrors(array('message' => 'Login field is required.')); 
2

這取決於你在哪裏捕捉異常。

Sentry不使用Validator類。因此,如果您想要通過Laravel方式返回錯誤消息,則應該先創建一個單獨的Validator對象並先進行驗證,然後在驗證通過後才傳遞給Sentry。

Sentry將只能傳回1個錯誤,因爲它捕捉特定的異常。此外,錯誤不會與驗證類中的錯誤類型相同。

此外,如果哨兵確實發現異常,那麼您的驗證顯然不起作用。下面

代碼是不是你應該怎麼做,但更多表現出的是什麼,我相信顯示了Laravel /哨兵工作

例用戶模型

class User extends Eloquent { 
    public $errors; 
    public $message; 

    public function registerUser($input) { 

     $validator = new Validator::make($input, $rules); 
     if $validtor->fails() { 
      $this->errors = $validator->messages(); 
      return false; 
     } 

     try { 
      // Register user with sentry 
      return true; 
     } 
     catch (Cartalyst\Sentry\Users\LoginRequiredException $e) 
     { 
      $this->message = "failed validation"; 

      return false; 
     } 
     } 
    } 
} 

UserController的

相結合的方式
class UserController extends BaseController { 

public function __construct (User $user) { $this->user = $user; } 

public function postRegister() 
{ 
    $input = [ 
     'email' => Input::get('email'), 
     'password' => Input::get('password'), 
     'password_confirmation' => Input::get('password_confirmation') 
    ]; 

    if ($this->user->registerUser($input)) { 
     Session::flash('success', 'You have successfully registered. Please check email for activation code.'); 
     return Redirect::to('/'); 
    } 
    else { 
     Session::flash('error', $this->user->message); 
     return Redirect::to('login/register')->withErrors($this->user->errors)->withInput(); 
    } 
} 
+0

你有語法錯誤,請檢查你的代碼。 –

+0

很久以前被打出來,並且是徒手輸入的 - 不是從運行代碼複製和粘貼。被寫爲概念證明。 – Gravy

+0

對不起,但這不是理由,它是相同的(或更糟糕的),因爲不提供代碼。錯誤的代碼阻止了那些只想要工作的人*。這不是年齡,這是5個月前。 –

相關問題