我使用 make useextend
函数在 Laravel 4 的验证类上扩展和添加自定义规则。
Validator::extend('foo', function($attribute, $value, $parameters)
{
return $value == 'foo';
});
当我使用新创建的自定义扩展验证规则时,validation.foo
如果规则失败,它会返回。在 Laravel 4 中扩展验证类时,有没有办法定义通用/默认消息?
我使用 make useextend
函数在 Laravel 4 的验证类上扩展和添加自定义规则。
Validator::extend('foo', function($attribute, $value, $parameters)
{
return $value == 'foo';
});
当我使用新创建的自定义扩展验证规则时,validation.foo
如果规则失败,它会返回。在 Laravel 4 中扩展验证类时,有没有办法定义通用/默认消息?
Laravel 4 文档特别声明您需要为自定义规则定义错误消息。
你有两个选择;
选项1:
$messages = array(
'foo' => 'The :attribute field is foo.',
);
$validator = Validator::make($input, $rules, $messages);
选项 2:
在语言文件中指定您的自定义消息,而不是将它们直接传递给验证器。为此,请将您的消息添加到 app/lang/xx/validation.php 语言文件中的自定义数组中:
'custom' => array(
'foo' => array(
'required' => 'We need to know your foo!',
),
),
如果有人想知道 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.",
date_not_in_future
您的自定义函数在哪里validateDateNotInFuture
。custom
每次你对任何字段使用规则时,Laravel 都会选择消息,除非你想覆盖特定字段的全局消息,否则你不必使用数组。
实现验证器的完整代码如下。
自定义验证器(带有 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 文件:
<?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()
{
}
}
然后只需在 config/app.php 中添加一行:
'App\Providers\RouteServiceProvider',
'App\Providers\ValidatorServiceProvider', // your custom validation
除了 TheShiftExchange 所说的之外,如果您查看该 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',
),
),
请注意自定义验证规则中的 'required' 和 'exists' 键如何与上述验证器中的键匹配。