Stevan Tosic
Stevan Tosic

Reputation: 7199

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)
    {
        $builder
            ->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?

Upvotes: 1

Views: 5937

Answers (2)

sams_67
sams_67

Reputation: 31

{# don't render unrendered fields #}

{{ form_end(form, {'render_rest': false}) }}

It is working, of course don't forget with this

{{ form_row(form._token) }}

Upvotes: 2

mansoor.khan
mansoor.khan

Reputation: 2616

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}) }}

Upvotes: 10

Related Questions