如何解决Cakephp自定义验证方法返回False,但不保存也不显示任何消息
我在ValidationDefault下具有以下验证器,以检查有效日期是否在有效日期之后。如果为false,则应显示该消息。
$validator
->add('effective_until','custom',['rule' => 'checkEffectiveDateRange','provider' => 'table','message' => 'The effective until date must come after the effective on date.']);
我在同一张表中具有以下自定义函数,但是即使我故意将有效日期设置为早于有效日期,它也会保存数据并且不显示验证错误消息。我在这里做错什么了吗?
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);
}
}
解决方法
对于任何可能需要此操作的人,我通过将验证器更改为以下内容来解决了此问题
验证器
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;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。