在this线程中,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;
}
}在twig中,我编写了form_errors(myForm),但没有显示错误。验证器工作正常,但没有设置错误。
有什么想法吗?
发布于 2013-12-30 04:16:13
请参阅Class Constraint文档。类约束被传递给一个对象,而不是一个简单的值。你在你的实体上设置了正确的注释吗?例如,
use My\Namespace\Validator\Constraints as MyAssertions;
/**
* @MyAssertions\ExistEmailValidator
*
*/
class MyEntity
{
protected $id;
protected $email;
// etc...
}在您的验证器中,您将收到一个MyEntity实例,您可以从中检索电子邮件值并对其进行验证。正如在上面的评论中提到的,您可能也想使用addViolationAt。
我希望这对你有所帮助,并且我正确地理解了你的情况。不过,我相信有更好的解释。
https://stackoverflow.com/questions/20819344
复制相似问题