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;
@doctrine.orm.entity_manager... run thecontainer:debugcommand on your console and ensure that yours is calleddoctrine.orm.default_entity_manager. If that doesn't turn out to be the problem, I'm as stuck as you - looks perfect otherwise. - Ragdata