formssymfonyvalidationsymfony4multi-step

Symfony4 - Validate fields based on values of other fields


I am building a very large form with multisteps and some conditional fields and cannot find a good solution for my problem.

For example if field A is "Foo" then field B is shown via Javascript and must be validated via Symfony, otherwise the field must be empty. Is there a Way to do this?

PS: Is https://github.com/craue/CraueFormFlowBundle a good way to go for complex multistep forms and should I use groups and entities or build it without a class? Whats the best practice?

Thanks!


Solution

  • My solution:

    I think you should use Symfony Form Events. In events you can do such custom stuff like adding errors, check values in all form, remove and add validators/fields dynamically.

    More about form events flow you can read here

    Here Add error to Symfony 2 form element you have question howto add error to field from event listener(In symfony 4 it is done similarly)

    My opinion about craue/CraueFormFlowBundle:

    I use CraueFormFlowBundle in my projects to create complicated multistep forms. It's useful Bundle and saves a lot of time. I created a lot of forms based on CraueFormFlowBundle + Doctrine entities in combination with validation groups + symfony form events - powerful and flexible solution which I can recommend