laravel - 在自定义验证规则的消息中包含参数
问题描述
我创建了一个自定义验证规则:
<?php
namespace App\Rules;
use Carbon\Carbon;
use Illuminate\Contracts\Validation\Rule;
class NotOlderThan
{
public function validate($attribute, $value, $parameters, $validator)
{
$maxAge = $parameters[0];
$date = Carbon::parse($value);
return !Carbon::now()->subYears($maxAge)->gte($date);
}
}
我已将其添加到我的 ServiceProvider 中:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Validator;
class RulesServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
Validator::extend('phone', 'App\\Rules\\Phone');
Validator::extend('not_older_than', 'App\\Rules\\NotOlderThan');
}
}
我已修改resources/lan/en/validation.php
以包括以下内容:
/*
|--------------------------------------------------------------------------
| Custom Validation Language Lines
|--------------------------------------------------------------------------
|
| Here you may specify custom validation messages for attributes using the
| convention "attribute.rule" to name the lines. This makes it quick to
| specify a specific custom language line for a given attribute rule.
|
*/
'phone' => 'The :attribute must be a valid :locale number without the country code prefix.',
'not_older_than' => 'Age cannot be older than :maxAge',
现在我可以像这样使用这些自定义验证规则:
$this->validate([
'phone' => 'required|phone',
'dateOfBirth' => 'not_older_than:30',
'issuedAt' => 'not_older_than:10'
]);
现在我遇到的问题是我希望能够在返回给客户端的验证消息中包含参数,但我不确定在哪里设置。例如。'not_older_than' => 'Age cannot be older than :maxAge'
应该Age cannot be older than 30 years.
在上面的例子中返回。
解决方案
因此,第二次查看文档时,我看到了以下内容,我一定是第一次错过了:
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Validator::extend(...);
Validator::replacer('foo', function ($message, $attribute, $rule, $parameters) {
return str_replace(...);
});
}
然而,这对我不起作用,由于某种原因,每当我添加此代码自定义验证器停止工作时,没有错误它只是不起作用。所以我环顾四周,在这里找到了一个解决方案,建议我尝试这种方式:
<?php
namespace App\Rules;
use Carbon\Carbon;
use Illuminate\Contracts\Validation\Rule;
class NotOlderThan
{
public function validate($attribute, $value, $parameters, $validator)
{
$validator->addReplacer('not_older_than', function ($message, $attribute, $rule, $parameters) {
return str_replace(':age', $parameters[0], $message);
});
$maxAge = $parameters[0];
$date = Carbon::parse($value);
return Carbon::now()->subYears($maxAge)->lte($date);
}
}
在这里,我在自定义规则类的 validate 方法中调用验证器实例,并在其上使用替换器。这对我有用。
推荐阅读
- spring-boot - Glassfish 中的 Spring Websockets - WebSocket 握手期间出错:发送非空“Sec-WebSocket-Protocol”标头但未收到响应
- django-channels - 在 Django Channels 中的 Django Rest 框架中获取会话集
- java - 如何修复使用#2nd 记录而不是#1st(#3rd 而不是#2nd 等)的游标?
- gradle - 如何在 gradle 5+ 中为 shadowJar 插件指定归档分类器
- curl - 使用 LexikJWTAuthenticationBundle 和 Symfony 3.4 创建身份验证令牌
- r - x$data[i, j] <- value 中的错误:要替换的项目数不是替换长度的倍数
- c# - 生成的pdf文件的分辨率
- html - 如何最好地创建这个箭头形状的 div/按钮?
- maven - 在不同的 Maven 模块中读取文件内容
- smt - 为什么CVC4产品运营商不适用于套装?