验证错误的用户名或电子邮件错误


Laravel 4 Auth wrong username or email error

我想给用户一个视觉反馈,如果他们输入错误的登录信息。

public function doLogin()
{
  $rules = array(
    'email'    => 'required|email',
    'password' => 'required|min:3'
  );
  $validator = Validator::make(Input::all(), $rules);
  if ($validator->fails()) {
    return Redirect::route('login')
      ->withErrors($validator)
      ->withInput(Input::except('password'));
  } else {
    $userdata = array(
      'email' => Input::get('email'),
      'password' => Input::get('password')
    );
    if (Auth::attempt($userdata)) {
      return Redirect::route('dashhome')
        ->withJsd('true');
    } else {
      return Redirect::route('login')
        ->withErrors(['wrongpw','Wrong E-mail address or Password']);
    }
  }
}

在我的登录视图中,我有这个代码

@if ($errors->has('wrongpw'))
  <script>
    $.gritter.add({
      title: 'Ups!',
      text: "{{ $errors->first('wrongpw') }}",
      class_name: 'warning',
      time: ''
    });
  </script>
@endif

但这不起作用。有没有人知道我做错了什么,或者有什么建议如何做得更好?

谢谢

我认为你没有在正确的关联数组中传递错误,

->withErrors(array('wrongpw' => 'Wrong E-mail address or Password'));

->withErrors(['wrongpw' => 'Wrong E-mail address or Password']);