Doctrine EntityManager未注入Custom Validator

时间:2015-01-23 03:55:45

标签: symfony doctrine-orm validation entitymanager symfony-2.5

我创建了一个自定义验证程序来检查两个数据库表中是否存在电子邮件。

namespace AgriHealth\AhpBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
 * @Annotation
 */
class UniqueEmailAll extends Constraint
{
    public $message = 'This email is already in use';

    public function validatedBy()
    {
        return get_class($this).'Validator';
    }
} 

namespace AgriHealth\AhpBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class UniqueEmailAllValidator extends ConstraintValidator
{
    private $entityManager;

    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }


    public function validate($value, Constraint $constraint)
    {
        $find1 = $this->entityManager->getRepository( 'AgriHealthAhpBundle:Vet')->findOneByEmail($value);
        $find2 = $this->entityManager->getRepository( 'AgriHealthAhpBundle:Farmer')->findOneByEmail($value);

        if ($find1 || $find2) {
            // If you're using the new 2.5 validation API (you probably are!)
            $this->context->buildViolation($constraint->message)
                ->addViolation();
        }
    }
}

我正在尝试将Doctrine Entity Manager注入验证器,如下所示:

services:
    validator.unique.UniqueEmailAll:
        class:  AgriHealth\AhpBundle\Validator\Constraints\UniqueEmailAllValidator
        arguments:
            entityManager: "@doctrine.orm.entity_manager"
        tags:
            - { name: validator.constraint_validator, alias: UniqueEmailAllValidator }

但这似乎不起作用:

捕获致命错误:参数1传递给AgriHealth \ AhpBundle \ Validator \ Constraints \ UniqueEmailAllValidator :: __ construct()必须是AgriHealth \ AhpBundle \ Validator \ Constraints \ EntityManager的实例,没有给出,在/ home / jochen /中调用项目/ ahp / trunk / vendor / symfony / symfony / src / Symfony / Bundle / FrameworkBundle / Validator / ConstraintValidatorFactory.php在第71行并在/ home / jochen / projects / ahp / trunk / src / AgriHealth / AhpBundle / Validator /中定义Constraints / UniqueEmailAllValidator.php第18行

我错过了什么?在config.yml中,我不确定应该调用什么服务。 Cookbook说要为validator.unique加前缀,但我不明白这个独特之处是什么?

1 个答案:

答案 0 :(得分:5)

首先在UniqueEmailAllValidator中你应该声明使用EntityManager

namespace AgriHealth\AhpBundle\Validator\Constraints;

use Doctrine\ORM\EntityManager;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Constraint;

class UniqueEmailAllValidator extends ConstraintValidator
{
 ...
}

其次在UniqueEmailAll中定义validatedBy函数,如此

class UniqueEmailAll extends Constraint
{
    public $message = 'This email is already in use';

    public function validatedBy()
    {
        return 'unique.email.all.validator';
    }
} 

在services.yml文件中执行此操作

services:
    validator.unique.UniqueEmailAll:
        class:  AgriHealth\AhpBundle\Validator\Constraints\UniqueEmailAllValidator
        arguments:
            entityManager: "@doctrine.orm.entity_manager"
        tags:
            - { name: validator.constraint_validator, alias: unique.email.all.validator }

这将解决您的问题。我不知道造成这个问题的第二部分有什么问题。也许它与大写字符有关。