Doctrine EntityManager 未注入自定义验证器
Doctrine EntityManager not injected into Custom Validator
我创建了一个自定义验证器来检查电子邮件是否不存在于 2 个数据库表中。
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 实体管理器注入验证器:
services:
validator.unique.UniqueEmailAll:
class: AgriHealth\AhpBundle\Validator\Constraints\UniqueEmailAllValidator
arguments:
entityManager: "@doctrine.orm.entity_manager"
tags:
- { name: validator.constraint_validator, alias: UniqueEmailAllValidator }
但这似乎不起作用:
可捕获的致命错误:传递给 AgriHealth\AhpBundle\Validator\Constraints\UniqueEmailAllValidator::__construct() 的参数 1 必须是给定的 AgriHealth\AhpBundle\Validator\Constraints\EntityManager、none 的实例,在 [=22 中调用=].php 在第 71 行,在 /home/jochen/projects/ahp/trunk/src/AgriHealth/AhpBundle/Validator/Constraints/UniqueEmailAllValidator.php 第 18
行中定义
我错过了什么?在 config.yml 中,我不确定应该调用什么服务。 Cookbook 说前缀 validator.unique 但我不明白 unique 指的是什么?
首先,您应该在 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 }
这将解决您的问题。我不知道导致此问题的第二部分有什么问题。可能跟大写字母有关
我创建了一个自定义验证器来检查电子邮件是否不存在于 2 个数据库表中。
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 实体管理器注入验证器:
services:
validator.unique.UniqueEmailAll:
class: AgriHealth\AhpBundle\Validator\Constraints\UniqueEmailAllValidator
arguments:
entityManager: "@doctrine.orm.entity_manager"
tags:
- { name: validator.constraint_validator, alias: UniqueEmailAllValidator }
但这似乎不起作用:
可捕获的致命错误:传递给 AgriHealth\AhpBundle\Validator\Constraints\UniqueEmailAllValidator::__construct() 的参数 1 必须是给定的 AgriHealth\AhpBundle\Validator\Constraints\EntityManager、none 的实例,在 [=22 中调用=].php 在第 71 行,在 /home/jochen/projects/ahp/trunk/src/AgriHealth/AhpBundle/Validator/Constraints/UniqueEmailAllValidator.php 第 18
行中定义我错过了什么?在 config.yml 中,我不确定应该调用什么服务。 Cookbook 说前缀 validator.unique 但我不明白 unique 指的是什么?
首先,您应该在 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 }
这将解决您的问题。我不知道导致此问题的第二部分有什么问题。可能跟大写字母有关