在Laravel 4中扩展Validation类时如何指定默认错误消息 [英] How to specify the default error message when extending the Validation class in Laravel 4
问题描述
我使用made使用extend
函数来扩展和在Laravel 4的Validation Class上添加自定义规则.
I use made use the extend
function to extend and adding custom rules on the Validation Class of Laravel 4.
Validator::extend('foo', function($attribute, $value, $parameters)
{
return $value == 'foo';
});
当我使用新创建的自定义扩展名验证规则时,如果规则失败,它将返回validation.foo
.在Laravel 4中扩展验证类时,是否可以定义通用/默认消息?
When I validate the rule using the newly created custom extension, it returns validation.foo
if the rule fails. Is there a way to define a generic/ default message when extending the validation class in Laravel 4?
推荐答案
Laravel 4文档明确说明,您需要为自定义规则定义错误消息.
The Laravel 4 docs specifically state you need to define an error message for your custom rules.
您有两个选择;
选项1:
$messages = array(
'foo' => 'The :attribute field is foo.',
);
$validator = Validator::make($input, $rules, $messages);
选项2:
在语言文件中指定您的自定义消息,而不是将其直接传递给Validator.为此,请将您的消息添加到app/lang/xx/validation.php语言文件中的自定义数组:
Specify your custom messages in a language file instead of passing them directly to the Validator. To do so, add your messages to custom array in the app/lang/xx/validation.php language file:
'custom' => array(
'foo' => array(
'required' => 'We need to know your foo!',
),
),
这篇关于在Laravel 4中扩展Validation类时如何指定默认错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!