繁体   English   中英

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

[英]Add custom error to symfony validator

我使用Doctrine 2和Symfony 2验证程序组件(独立,不带表单组件)。

因此,当我完成对Doctrine实体的检查并将其传递给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"));

它看起来应该像这样:

$error = new ConstraintViolation('Error message', '', [], $entity, 'fieldName', 'value that caused this violation');
$errors->add($error);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM