I have a legacy project that is not a symfony2 project, yet has the symfony2 components available.
I have Doctrine entities, and I want to be able to assert them via annotations. I do not have a container and cannot just call:
$container->get('validator')->validate($entity);
You can initialize the Validator via:
And you validate an entity via:
If
$violations
is an empty array, the entity was validated, otherwise you will get the violations and can:You assert your entity and make sure that all the annotations are used. The legacy project I was using e.g. did not include the
@Entity
annotation, and while it didn't bother Doctrine, it did bother the validation process.And finally, you must autload the annotations. Doctrine will not use the default autoloader, you have to specifically use the
Doctrine\Common\Annotations\AnnotationRegistry
You can do it via: