guillaumeoriol/serquant — Issue #11: write a factory to get Symfony Validation component as a service
Author: guillaumeoriol
Created: 2011-10-26 10:16:30+00:00
State: open
Labels: enhancement

An issue was raised when combining Doctrine with Symfony validator.
When I switched to Doctrine 2.1, I had this error message:

> The annotation "@Symfony\Component\Validator\Constraints\NotBlank"
> in property XXX does not exist, or could not be auto-loaded.

The solution was found in [Doctrine common 2.1 documentation](http://www.doctrine-project.org/docs/common/2.1/en/reference/annotations.html).

Doctrine Annotations (which is part of Doctrine Common project and used by Symfony validator as well as Doctrine ORM) uses its own autoloading mechanism. Without any specific directive, Symfony validator classes are not registered and thus raise the above error.

We must add the following code to register Symfony validator constraints:

use Doctrine\Common\Annotations\AnnotationRegistry;
AnnotationRegistry::registerAutoloadNamespace(
'Symfony\Component\Validator\Constraints', APPLICATION_ROOT . '/library');

This piece of code should only be added if annotations are used by Symfony validator.
This may be the reason for [Symfony Validation documentation](http://symfony.com/doc/2.0/book/validation.html#configuration) stating:

> The Symfony2 validator is enabled by default, but you must explicitly enable annotations
> if you're using the annotation method to specify your constraints.

Then, the problem is to write a factory for the *Symfony Validation component* that will build a service (taking into account the special case of annotations) and give access to it as any other services. The `Serquant\Service\Crud` class should be updated to get this validation service from the DIC.