如何添加自定义错误?

时间:2014-01-17 15:51:23

标签: php laravel laravel-4

我认为在登录时显示所有验证错误并且它工作正常,但是如果电子邮件/传递不正确,我想添加另一个验证错误。我已经关注了一些信息,但似乎没有用。

$validation = Validator::make(Input::all(), array(
    'email' => 'required|email',
    'password' => 'required|min:6'
));

if ($validation->fails()) {
    // These errors work just fine and are showing
    return Redirect::to('login')->withErrors($validation);
}

// .. some code to check user credentials etc..

// Here I want to imitate validation error
// I don't even have "workaround" on my form so it should always be null
// But the errors won't show
$validation = Validator::make(Input::all(), array('workaround' => 'required'), array('workaround' => 'Wrong email and/or password.'));
$validation->fails();
return Redirect::to('login')->withErrors($validation);

查看

@if (count($errors) > 0)
    <div class="errrors">
        <ul>
            @foreach ($errors->all() as $message)
            <li>{{ $message }}</li>
            @endforeach
        </ul>
    </div>
@endif

1 个答案:

答案 0 :(得分:1)

此处的消息是针对验证器workaround的。我想你期待它是为了这个领域。

$validation = Validator::make(Input::all(), array('workaround' => 'required'), array('workaround' => 'Wrong email and/or password.'));

将其更改为required以将正确的错误消息绑定到支票。

$validation = Validator::make(Input::all(), array('workaround' => 'required'), array('required' => 'Wrong email and/or password.'));