我使用Doctrine 2和Symfony 2 Validator Component(独立,没有Forms Component)。
因此,当我完成检查Doctrine Entity并将其传递给SF2 Validator时,我需要向验证器添加自定义错误消息。我怎么能这样做?
到目前为止,这是我的代码:
$validator = Validation::createValidatorBuilder()->enableAnnotationMapping()->getValidator();
$errors = $validator->validate($entry);
// Add custom error will be here
$errors->add(new ConstraintViolation("Error text maybe here"));
答案 0 :(得分:2)
看起来应该是这样的:
$error = new ConstraintViolation('Error message', '', [], $entity, 'fieldName', 'value that caused this violation');
$errors->add($error);