pikachu0
pikachu0

Reputation: 812

Constraint Validator validates Entity before Entity is ready in Symfony 2

My Symfony2 (2.4.2) application has a form, and an entity which I have set some validation constraints on some fields. One of the entity's variables is an array which should not be blank.

The form has a field which is not mapped to the entity directly. The input is a comma-delimited string; this string will be preg_split into an array and saved to the instance variable aforementioned. This operation is triggered by FormEvents::POST_SUBMIT.

However, when I submit the form, even though the input string is not empty, the form shows that the validation fails. I did a bit of debugging and found that the validation actually happens before FormEvents::POST_SUBMIT. I have tried other FormEvents but no luck.

Is there a way to trigger the event before the validation?

Note: The scenario above is shortened, it'd be too long for me to ask a question if the context is my real application.

Upvotes: 1

Views: 909

Answers (1)

Cerad
Cerad

Reputation: 48883

Use DataTransformers for this sort of operation. Not form events.

The data transformer will kick in before the validation. The transformer will transform whatever the user types into whatever you expect internally. The validation will then operate on the internal data format.

http://symfony.com/doc/current/cookbook/form/data_transformers.html

Upvotes: 2

Related Questions