Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply validation before Data Transform

Tags:

forms

php

symfony

I want to transform a text field that I got from submitted user data to an Object in Symfony2. I used DataTransformer in order to do this. When I use built-in validators like 'NotEmpty' or 'NotNull' or any custom validators that built in standard way Symfony2 passes my specific object to them but I want to validate this text field before converting it to object. What should I do? (sry if my English is not so good)

like image 659
Lost Koder Avatar asked Aug 08 '14 10:08

Lost Koder


1 Answers

Validation are always done on the reverse transformed data.

The best way to add validation rule before the transformation occurred is to use an event listener or subscriber on FormEvents::PRE_SUBMIT.

You'll get the raw data. Just apply your validation logic here then use the $event->getForm()->get('xxxx')->addError() method to add errors on the corresponding field.

More informations on event subscribers / listeners :

http://symfony.com/doc/current/components/form/form_events.html#event-listeners http://symfony.com/doc/current/components/form/form_events.html#event-subscribers

like image 180
rolebi Avatar answered Oct 17 '22 22:10

rolebi