0
votes

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.

The problem is that after doing everything by the book I am still getting an error saying that the entityManager is 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

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;
1
My EntityManager dependency is called @doctrine.orm.entity_manager ... run the container:debug command on your console and ensure that yours is called doctrine.orm.default_entity_manager. If that doesn't turn out to be the problem, I'm as stuck as you - looks perfect otherwise. - Ragdata
Have you tried clearing your cache and restarting php? - jcroll

1 Answers

0
votes

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

There are actually 2 mistakes in my code.

Calling of validatedBy() function

This function should be called inside the IsSingleEntry class and not IsSingleEntryValidator

Method name should be diferent

I call the method validateBy() but the correct function name should be validatedBy()

So the code should be looking like this now:

IsSingleEntry

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

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

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();
        }
    }
}