在 Laravel 4 中扩展 Validation 类时如何指定默认错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17647044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to specify the default error message when extending the Validation class in Laravel 4
提问by Abishek
I use made use the extend
function to extend and adding custom rules on the Validation Class of Laravel 4.
我使用 madeextend
函数在 Laravel 4 的验证类上扩展和添加自定义规则。
Validator::extend('foo', function($attribute, $value, $parameters)
{
return $value == 'foo';
});
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?
当我使用新创建的自定义扩展验证规则时,validation.foo
如果规则失败,它将返回。在 Laravel 4 中扩展验证类时,有没有办法定义通用/默认消息?
回答by Laurence
The Laravel 4 docs specifically stateyou need to define an error message for your custom rules.
Laravel 4 文档明确指出您需要为自定义规则定义错误消息。
You have two options;
你有两个选择;
Option 1:
选项1:
$messages = array(
'foo' => 'The :attribute field is foo.',
);
$validator = Validator::make($input, $rules, $messages);
Option 2:
选项 2:
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:
在语言文件中指定您的自定义消息,而不是将它们直接传递给验证器。为此,请将您的消息添加到 app/lang/xx/validation.php 语言文件中的自定义数组中:
'custom' => array(
'foo' => array(
'required' => 'We need to know your foo!',
),
),
回答by JustAMartin
In case someone is wondering about Laravel 5: just add your message to validation.php right under all the default messages. For example:
如果有人想知道 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.",
where date_not_in_future
is your custom function validateDateNotInFuture
.
Laravel will pick the message each time you use your rule for any field and you won't have to use custom
array unless you want to override your global message for specific fields.
date_not_in_future
你的自定义函数在哪里validateDateNotInFuture
。每次您将规则用于任何字段时,Laravel 都会选择消息,custom
除非您想覆盖特定字段的全局消息,否则您不必使用数组。
Full code to implement the validator follows.
实现验证器的完整代码如下。
Custom Validator (with a bonus gotcha comments for date_format and date_before localization):
自定义验证器(带有 date_format 和 date_before 本地化的额外陷阱评论):
<?php namespace App\Services\Validation;
use Illuminate\Validation\Validator as BaseValidator;
/**
* Class for your custom validation functions
*/
class Validator extends BaseValidator {
public function validateDateNotInFuture($attribute, $value, $parameters)
{
// you could also test if the string is a date at all
// and if it matches your app specific format
// calling $this->validateDateFormat validator with your app's format
// loaded from \Config::get, but be careful -
// Laravel has hard-coded checks for DateFormat rule
// to extract correct format from it if it exists,
// and then use for validateBefore. If you have some unusual format
// and date_format has not been applied to the field,
// then validateBefore will give unpredictable results.
// Your best bet then is to override protected function
// getDateFormat($attribute) to return your app specific format
$tomorrow = date('your app date format here', strtotime("tomorrow"));
$parameters[0] = $tomorrow;
return $this->validateBefore($attribute, $value, $parameters);
}
}
ValidatorServiceProvider file:
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()
{
}
}
And then just add a line to config/app.php:
然后在 config/app.php 中添加一行:
'App\Providers\RouteServiceProvider',
'App\Providers\ValidatorServiceProvider', // your custom validation
回答by omar j
In addition to what TheShiftExchange has said, if you look in that validation.php language file you'll see all of the different rules that you can specify. So for instance, if your validator has entries like this:
除了 TheShiftExchange 所说的之外,如果您查看该validation.php 语言文件,您将看到可以指定的所有不同规则。例如,如果您的验证器有这样的条目:
class ArticleValidator extends Validator
{
public static $rules = [
'create' => [
'title' => ['required'],
'slug' => ['required', 'regex:([a-z'custom' => array(
'company_article_type_id' => array(
'required' => 'The slug field is really important',
'exists' => 'The slug already exists',
),
),
-9\-]*)']
]
];
}
Then your custom validation rules may look like this:
那么您的自定义验证规则可能如下所示:
##代码##Notice how the 'required' and 'exists' keys in the custom validation rules match those in the validator above.
请注意自定义验证规则中的 'required' 和 'exists' 键如何与上述验证器中的键匹配。