1

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!

1 Answers1

1

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

Krzysztof Raciniewski
  • 4,735
  • 3
  • 21
  • 42
  • Hi, sounds great. I don't need Doctrine entities though, because my data is send to an api endpoint, but non db entities should work the same or not? – André Rinas Feb 18 '19 at 08:37
  • Doctrine has nothing to do with it. You can create simple model(simple PHP class) and it should work – Krzysztof Raciniewski Feb 18 '19 at 08:42
  • Ok. Thanks! A last question. Is it possible to generate a form based on a configuration file? For example I have a json file with multiple arrays and fields can I create a form dynamicly? My Entities might be different and the order of the fields might change sometimes. – André Rinas Feb 18 '19 at 09:17
  • This can be problematic because you create a form based on the model. I would create several models and apply inheritance for similar entities – Krzysztof Raciniewski Feb 18 '19 at 09:38
  • I already feared that. My need is to create a form-builder with a frontend in the future, where I can drag and drop fields, or groups. Maybe this could work: https://symfony.com/doc/current/form/without_class.html – André Rinas Feb 18 '19 at 09:47
  • You must try :) Good luck – Krzysztof Raciniewski Feb 18 '19 at 10:13
  • @KrzysztofRaciniewski How do you reload your form with Craue to update the form with values it contains ? In my case it only reload when I. go to the next step ... Do you have a EventListener. example using CraueFlow ? – Gauthier Feb 18 '21 at 13:10