In a form, I dynamically define the fields in a controller. Then in the TWIG I would like to use form_widget to output the fields.
The normal approach, when knowing the fields is like shown bellow:
{{ form_widget(form.field1) }}
In this case, we cannot know whether we will have a field1, field2, etc. on beforehand. Though we have the field names in a variable called key.
So what we would like to achieve is replace the hardcoded field1 by something dynamic.
You can do something like
{{ form_widget(form[key]) }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With