Symfony 自定义约束不显示消息

Symfony 自定义约束不显示消息,symfony,custom-validators,Symfony,Custom Validators,在线程中,im与自定义验证器进行斗争。感谢stackoverflow用户,我可以使用验证程序。现在,我不能这样做显示错误。 验证程序类是: class ExistEmailValidator extends ConstraintValidator { protected $userService; public function setUserService($us) { $this->userService = $us; } publ

在线程中,im与自定义验证器进行斗争。感谢stackoverflow用户,我可以使用验证程序。现在,我不能这样做显示错误。 验证程序类是:

class ExistEmailValidator extends ConstraintValidator
{

    protected $userService;

    public function setUserService($us) {
        $this->userService = $us;
    }

    public function validate($value, Constraint $constraint)
    {
        if($this->userService->existUserEmail($value) == false){
            $this->context->addViolation($constraint->message, array('%string%' => $value));
        }

    }

    public function getTargets(){
        return self::CLASS_CONSTRAINT;
    }

}
我在细枝上写了
表单错误(myForm)
,但错误没有显示出来。验证器工作正常,但未设置错误

有什么想法吗?

请参阅文档。类约束被传递给一个对象,而不是一个简单的值。是否在实体上设置了正确的注释?比如说,

use My\Namespace\Validator\Constraints as MyAssertions;

/**
 * @MyAssertions\ExistEmailValidator
 *
 */
class MyEntity
{
    protected $id;

    protected $email;

    // etc...

}
在验证器中,您将收到MyEntity的一个实例,您可以从中检索电子邮件值并对其进行验证。正如上面的评论中所提到的,您可能也希望使用addViolationAt

我希望这会有所帮助,并且我正确理解你的情况。不过,我相信还有更好的解释。

您可以尝试使用。