2013-08-21 23 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个月前。 –

相关问题