symfony2 dynamic form validation with the collection, depending on the values of the fields

For example I have a Post document, which can be a simple post with a collection of paragraphs and post the link. Post can be created as a link and later to be edited, filled with original content (paragraphs, which is a collection)
Post fields: title, is_external, external_url, paragraphs.
A paragraph consists of: title, description. Description mandatory.

How can I validate values from the form according to the following rules:
If is_external checked, external_url cannot be empty, and paragraphs you do not check. Otherwise, external_url needs to be empty, a collection of paragraphs must contain at least one entry, all sections must be validated.

How to do it properly?
October 3rd 19 at 02:33
3 answers
October 3rd 19 at 02:35
Myself kolbet to providerbut?
symfony.com/doc/current/reference/constraints/Callback.html
Or custom fact to write?
symfony.com/doc/current/cookbook/validation/custom_constraint.html
Sorry if you did not understand.
I decided to act. Wrote colback, which well validates external_url, but a collection of paragraphs of a problem.
Rested in the validation of the collection, depending on the field. Don't know how to call a validator deeper. - verda.MacGyver commented on October 3rd 19 at 02:38
October 3rd 19 at 02:37
One good approach is create your own validator that will validate not a property and, zelly class. symfony.com/doc/current/cookbook/validation/custom_constraint.html the big plus Here is that You can then, if necessary, to determine as a service, and inject some dependencies.

There is also the possibility to process this check on event FormEvents::SUBMIT (At >= 2.1 < 2.3 FormEvents::BIND), and if there is an error that to add it addError(new FormError(/**... **/));
October 3rd 19 at 02:39
Why do you need form validation? You must have validation of the final model.
And in my opinion it would be better to split it up into 2 entities and the sample to merit these entities
Okay, then tell me how to dynamically validate the model in a similar way.
If you mean two different classes, then this option is not suitable, because to switch the type of account you want on the fly with a checkbox. - verda.MacGyver commented on October 3rd 19 at 02:42
and what is the difficulty to switch the model type on the fly? - verda.MacGyver commented on October 3rd 19 at 02:45
Document Post should be one. He has, besides these, there are 10 fields that behave the same way, regardless is_external, and also for the system as a whole is_external have Post is irrelevant. To have 2 different models with 80% of the same functionality contrary to DRY. To complicate your life inheritance is not desirable, because again, both entities perform the same task. From the point of view of business logic is_external equivalent background color. - buck commented on October 3rd 19 at 02:48
So 3 models - verda.MacGyver commented on October 3rd 19 at 02:51
I don't understand. At all. - buck commented on October 3rd 19 at 02:54

Find more questions by tags Symfony