Validation 如何在Laravel 4中扩展验证类时指定默认错误消息

Validation 如何在Laravel 4中扩展验证类时指定默认错误消息,validation,laravel,laravel-4,Validation,Laravel,Laravel 4,我使用extend函数在Laravel4的验证类上扩展和添加自定义规则 Validator::extend('foo', function($attribute, $value, $parameters) { return $value == 'foo'; }); 当我使用新创建的自定义扩展验证规则时,如果规则失败,它将返回validation.foo。在Laravel 4中扩展验证类时,是否有方法定义通用/默认消息?您需要为自定义规则定义错误消息 你有两个选择 选项1: $messa

我使用
extend
函数在Laravel4的验证类上扩展和添加自定义规则

Validator::extend('foo', function($attribute, $value, $parameters)
{
    return $value == 'foo';
});
当我使用新创建的自定义扩展验证规则时,如果规则失败,它将返回
validation.foo
。在Laravel 4中扩展验证类时,是否有方法定义通用/默认消息?

您需要为自定义规则定义错误消息

你有两个选择

选项1:

$messages = array(
    'foo' => 'The :attribute field is foo.',
);

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

$messages = array(
    'foo' => 'The :attribute field is foo.',
);

$validator = Validator::make($input, $rules, $messages);
在语言文件中指定自定义消息,而不是将它们直接传递给验证器。为此,请将消息添加到app/lang/xx/validation.php语言文件中的自定义数组中:

'custom' => array(
    'foo' => array(
        'required' => 'We need to know your foo!',
    ),
),

除了Shift Exchange所说的之外,如果查看validation.php语言文件,您将看到可以指定的所有不同规则。例如,如果您的验证器有如下条目:

class ArticleValidator extends Validator
{
    public static $rules = [
        'create'    => [
            'title'                     => ['required'],
            'slug'                      => ['required', 'regex:([a-z\0-9\-]*)']
        ]
    ];

}
'custom' => array(
    'company_article_type_id' => array(
        'required' => 'The slug field is really important',
        'exists' => 'The slug already exists',
    ),
),
然后,您的自定义验证规则可能如下所示:

class ArticleValidator extends Validator
{
    public static $rules = [
        'create'    => [
            'title'                     => ['required'],
            'slug'                      => ['required', 'regex:([a-z\0-9\-]*)']
        ]
    ];

}
'custom' => array(
    'company_article_type_id' => array(
        'required' => 'The slug field is really important',
        'exists' => 'The slug already exists',
    ),
),

请注意自定义验证规则中的“required”和“exists”键与上述验证器中的键是如何匹配的。

如果有人想知道Laravel 5:只需将您的消息添加到validation.php的所有默认消息下面即可。例如:

<?php

return [
// .. lots of Laravel code omitted for brevity ...

"timezone"             => "The :attribute must be a valid zone.",

/* your custom global validation messages for your custom validator follow below */

"date_not_in_future"          => "Date :attribute may not be in future.", 
ValidatorServiceProvider文件:

<?php namespace App\Providers;

namespace App\Providers;

use App\Services\Validation\Validator;
use Illuminate\Support\ServiceProvider;

class ValidatorServiceProvider extends ServiceProvider{

    public function boot()
    {
        \Validator::resolver(function($translator, $data, $rules, $messages)
        {
            return new Validator($translator, $data, $rules, $messages);
        });
    }

    public function register()
    {
    }
}

谢谢,在浏览验证扩展时,我似乎完全错过了该部分。感谢您的帮助。我对
定制
解决方案有疑问。这是否意味着您必须为每个字段指定消息,而不是为您的自定义验证器的所有情况指定消息?在这种情况下,
custom.foo
可能被视为字段名,而不是您的
foo
验证器。而且文档还指出,
custom
应该只用于特定字段的自定义错误消息,例如
myField.required=>'message'`而不是
'myValidator=>'message'`。所以,我仍然在寻找解决方案,为什么我的自定义验证器不从validation.php文件中选择消息。我想是时候进行调试了。也许L5已经改变了它?上面的Laravel 5在参考资料/lang/xx,参考资料/lang/enth中,这些规则特定于“公司文章类型id”字段。但是,如果我想在validation.php文件中为完全自定义的验证函数的所有情况定义消息,该怎么办?出于某种原因,拉威尔5号没有收到我的信息。