如何在自定义验证器中添加错误


(Laravel) How to add errors in custom validators?

我遵循了这个简短的教程:http://blog.elenakolevska.com/laravel-alpha-validator-that-allows-spaces/.

现在我的问题是,我如何添加错误到它?因为现在我得到了一个错误:

validation.alpha_spaces

我也很好奇如何将数字添加到验证器?谢谢!

编辑:

如果你不想打开链接,这基本上是我的代码:

Validator::extend('alpha_spaces', function($attribute, $value) {
    return preg_match('/^['pL's]+$/u', $value);
});

您还需要传递一个自定义的错误消息,例如:

Validator::extend('alpha_spaces', function($attribute, $value) {
    // ...
});

现在为这个自定义规则准备一条消息:

$messages = array('alpha_spaces' => 'Your custom error message for :attribute');

像这样使用它(与$messages):

$validator = Validator::make($input, $rules, $messages);

这里:attribute将被替换为表单的字段名