I have a RegistrationFormType with following validation constraints:
public function getDefaultOptions(array $options)
{
$collectionConstraint = new Collection(array(
'email' => array(
new NotBlank(),
new Email(array('message' => 'Ungültige E-Mail Adresse')),
),
'username' => new Unique(),
'code' => new MaxLength(array('limit'=>20)),
'plainPassword' => new MaxLength(array('limit'=>20)),
));
return array(
'csrf_protection' => false,
'validation_constraint' => $collectionConstraint,
);
}
In order to assure uniqueness I created a Unique Class (extending Contraint) and a UniqueValidator (extending ConstraintValidator) like described here: http://www.michelsalib.com/2011/04/create-your-own-constraint-validator-in-symfony2-a-doctrine-unique-validator/
The problem is, I get following error on submitting the form:
Catchable Fatal Error: Argument 1 passed to ...\Validation\Constraint\UniqueValidator::__construct() must be an instance of Doctrine\ORM\EntityManager, none given
It seems that the EntityManager is not injected into the ConstraintValidator, I created a service definition in my config.yml though:
services:
eventiply.validator.unique:
class: Ajado\EventHubBundle\Validation\UniqueValidator
arguments:
entityManager: "@doctrine.orm.entity_manager"
tags:
- { name: validator.constraint_validator, alias: validator.unique }
Any ideas how I could progress here?
Found the problem:
I had following code in my Unique Constraint:
And this is the corrected one:
}