我在ValidationDefault下有以下验证器来检查effective_until日期是否在effective_on日期之后。如果为false,则应显示消息。
$validator
->add('effective_until', 'custom', ['rule' => 'checkEffectiveDateRange', 'provider' => 'table', 'message' => 'The effective until date must come after the effective on date.']);
我在同一个表中有以下自定义函数,但即使故意将effective_until日期设置为早于effective_on日期,它也保存数据,并且不显示验证错误消息。我在这里做错什么了吗?
public function checkEffectiveDateRange($check, $context)
{
if(array_key_exists('newRecord',$context))
{
return strtotime($context['data']['effective_on']) < strtotime($check);
}
else
{
return strtotime($context['effective_on']) < strtotime($check);
}
}
发布于 2020-09-01 14:55:05
对于任何可能需要这样做的人,我通过将验证器更改为下面的方法来解决这个问题。
Validator
public function validationDefault(Validator $validator): Validator
{
$validator
->add('effective_until', 'custom',
['rule' => function($check, $context) {
if($this->checkEffectiveDateRange($check, $context)) {
return true;
}
return false;
},
'message' => 'The effective until date must come after the effective on date.']);
return $validator;
}
https://stackoverflow.com/questions/63639111
复制相似问题