我很确定我错过了一些小东西,但该死的无法弄清楚......请帮帮我,伙计们:)
我已经扩展了应用服务提供商.php
public function boot()
{
//
Validator::extend('ageLimit', 'App'Http'CustomValidator@validateAgeLimit');
}
我创建了一个新的自定义验证器.php
<?php
namespace App'Http;
use DateTime;
class CustomValidator {
public function validateAgeLimit($attribute, $value, $parameters, $validator)
{
$today = new DateTime(date('m/d/Y'));
$bday = new DateTime($value);
$diff = $bday->diff($today);
$first_param = $parameters[0];
if( $diff->y >= $first_param ){
return true;
}else{
return false;
}
}
}
我在验证中添加了新行.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.
|
*/
'age_limit' => ':attribute -> Age must be at least :ageLimit years old.',
'custom' => [
'attribute-name' => [
'rule-name' => 'custom-message',
],
],
这是我的规则:
'birth_date' => 'required|date|ageLimit:15',
所有这些都工作正常...在验证中排除了参数:ageLimit.php文件..
我怎样才能到达我在规则 ??? 中传递的参数 15
因为我收到此消息:
Birth day -> Age must be at least :ageLimit years old.
当然,我想得到这个:
Birth day -> Age must be at least 15 years old.
在Validator::extend(...)
下面,您可以添加:
Validator::replacer('ageLimit', function($message, $attribute, $rule, $parameters) {
$ageLimit = $parameters[0];
return str_replace(':ageLimit', $ageLimit, $message);
});
https://laravel.com/docs/5.2/validation#custom-validation-rules
希望这有帮助!