简体   繁体   English

Symfony2自定义验证器与EntityManager不起作用

[英]Symfony2 Custom Validator with EntityManager not working

I am trying to build a validator that will check against the database for some values. 我正在尝试建立一个验证器,它将针对数据库检查某些值。 For this I need to inject inside a service the entityManager and give an alias to my Validation method as documented in Symfony official documentation. 为此,我需要在服务内部注入EntityManager,并为Symfony官方文档中记录的Validation方法赋予别名。

The problem is that after doing everything by the book I am still getting an error saying that the entityManager is null: 问题是在完成本书所有工作后,我仍然收到一条错误消息,指出entityManager为null:

Catchable Fatal Error: Argument 1 passed to XXX\\CommonBundle\\Validator\\Constraints\\IsSingleEntryValidator::__construct() must be an instance of Doctrine\\ORM\\EntityManager, none given, called in /var/www/XXX/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Validator/ConstraintValidatorFactory.php on line 71 and defined 可捕获的致命错误:参数1传递给XXX \\ CommonBundle \\ Validator \\ Constraints \\ IsSingleEntryValidator :: __ construct()必须是Doctrine \\ ORM \\ EntityManager的实例,没有给出,在/ var / www / XXX / vendor / symfony / symfony中调用/src/Symfony/Bundle/FrameworkBundle/Validator/ConstraintValidatorFactory.php在第71行并已定义

My service: 我的服务:

XXX.validators.is_single_entry:
    class: XXX\CommonBundle\Validator\Constraints\IsSingleEntryValidator
    arguments:
        - "@doctrine.orm.default_entity_manager"
    tags:
        - { name: validator.constraint_validator, alias: single_entry_validation }

And the validator class: 和验证器类:

class IsSingleEntryValidator extends ConstraintValidator
{

    /**
     * @var EntityManager
     */
    protected $em;

    /**
     * Constructor
     *
     * @param EntityManager $em
     */
    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    public function validate($value, Constraint $constraint)
    {
        ...
    }

    public function validateBy()
    {
        return 'single_entry_validation';
    }
}

And the use of validator: 和验证器的使用:

/**
 * @ORM\Column(name="is_primary", type="boolean", nullable=true)
 * @SiteAssert\IsSingleEntry(message="validator.single.entry")
 */
protected $isPrimary;

@Ragdata - doctrine.orm.default_entity_manager Doctrine\\ORM\\EntityManager @Ragdata-doctrine.orm.default_entity_manager教义\\ ORM \\ EntityManager

There are actually 2 mistakes in my code. 我的代码中实际上有2个错误。

Calling of validatedBy() function 调用validatedBy()函数

This function should be called inside the IsSingleEntry class and not IsSingleEntryValidator 此函数应在IsSingleEntry类内部而不是IsSingleEntryValidator中调用

Method name should be diferent 方法名称应不同

I call the method validateBy() but the correct function name should be validatedBy() 我调用方法validateBy(),但正确的函数名称应为validatedBy()

So the code should be looking like this now: 因此,代码现在应该看起来像这样:

IsSingleEntry IsSingleEntry

class IsSingleEntry extends Constraint
{
    public $message = "The value already exists in the database";

    /**
     * @return string
     */
    public function validatedBy()
    {
        return 'single_entry_validation';
    }
}

IsSingleEntryValidator IsSingleEntryValidator

class IsSingleEntryValidator extends ConstraintValidator
{

    /**
     * @var EntityManager
     */
    protected $em;

    /**
     * Construct
     *
     * @param EntityManager $em
     */
    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    /**
     * Validate
     *
     * @param mixed $value
     * @param Constraint $constraint
     */
    public function validate($value, Constraint $constraint)
    {
        $oActiveExists = $this->em->getRepository('DatabaseBundle:Languages')->findOneByIsPrimary(true);

        if ($oActiveExists) {
            $this->context->buildViolation($constraint->message)
                ->addViolation();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM