问题是我正在创建一个自定义验证器,在我的新验证器类中,我应该这样做:
class EmailDoesntExistValidator extends ConstraintAValidator
{
public function validate($value, Constraint $constraint)
{
$em = $this->getDoctrine()->getEntityManager(); // ERROR!!!!!!!
$result = $em->getRepository('CgboardSignupBundle:User')->userExist($value);
if (empty($result)) {
return true;
}
$this->context->addViolation($constraint->message, array());
return false;
}
}
我有这个错误:
Error: Call to undefined method Cgboard\SignupBundle\Validator\Constraints\EmailDoesntExistValidator::getDoctrine()
有什么想法吗?
答案 0 :(得分:4)
它不起作用,因为您需要注入EntityManager / Doctrine依赖项。
在此处阅读如何创建具有依赖关系的验证器: http://symfony.com/doc/current/cookbook/validation/custom_constraint.html#constraint-validators-with-dependencies