将自定义错误添加到symfony验证程序

问题描述 投票:1回答:1

我使用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"));
php symfony validation
1个回答
2
投票

它应该是这样的:

$error = new ConstraintViolation('Error message', '', [], $entity, 'fieldName', 'value that caused this violation');
$errors->add($error);
© www.soinside.com 2019 - 2024. All rights reserved.