Is there a way to use symfony2 object checks as doctrine checks?

I am developing a Symfony2 application that uses several forms. Form data is saved in MySQL db using Doctrine2. I set some restrictions for objects using symfony annotations. Now, when the user cannot enter the corresponding data into the form, he receives an error message, but when I try to manipulate the same objects using the Command object, I get no exceptions or errors at all.

From the documentation I read, Symfony and Doctrine validation works as separate mechanisms, now ... is there a way to make them work as one? What I'm trying to avoid is to write the same checks for entity objects in order to use them as authentication and backend. Thanks.

+4
source share
2 answers

Forms are checked automatically when you call $form->isValid() , but if you want to check the object outside the form, you must call it manually.

In your command class, just start the validator service and validate your object before you save it.

 $validator = $this->container->get('validator'); $errors = $validator->validate($myEntity); if (count($errors) > 0) { return new Response(print_r($errors, true)); } else { return new Response('The entity is valid!'); } 

Read more in the documentation here http://symfony.com/doc/master/book/validation.html#using-the-validator-service

+11
source

Of course you can: http://symfony.com/doc/current/book/validation.html#constraint-configuration

Click the Annotations tab of the sample. You can add a validation condition with ORM annotations in entities

-1
source

All Articles