Stevan Tosic Stevan Tosic - 1 month ago 25
Twig Question

Hiding unwanted Symfony form fields from twig

When showing Symfony form in twig, how to hide field that is not in form_widget?

{{ form_start(form) }}
{{ form_widget(form.field1) }}
{{ form_end(form) }}

And if I have field2 in form type it will show by default in twig by no matter that I had not insert in form_widget

class MessageFormType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options)
->add('field1', TextType::class, array())

->add('field2', TextType::class, array())

I am hiding those unwanted fieleds with css, bit I wonder if there some elegant solution?


From the docs, you need to pass a key,value pair 'render_rest' : false to the form_end tag.

{# don't render unrendered fields #}
{{ form_end(form, {'render_rest': false}) }}