How to render form_rest() as hidden fields in Symfony2/Twig?

Creasixtine picture Creasixtine · Mar 8, 2012 · Viewed 12.2k times · Source

I have a Form class that contains many fields. I would like to render few of them and pass the left ones as hidden. How is this possible ?

I would like to do something like {{ form_rest(form, {'display': 'hidden'}) }} or <div display="hidden">{{ form_rest(form) }}</div>.

Example :

<form action="{{ path('fiche_intervention', {'rreid': rre.rreid}) }}" method="post" {{ form_enctype(form) }}>
    {{ form_errors(form) }}
    <div class="bloc-input">{{ form_label(form.rredatecommencement, "Date de retrait :") }}
    {{ form_widget(form.rredatecommencement) }}
    </div>

{# Some other fields... #}
    {# ... #}
{# /Some other fields... #}     

    <div display="hidden">{{ form_rest(form) }}</div>
    <input type="submit" />
</form>

Answer

unairoldan picture unairoldan · Mar 8, 2012

You have to do it in you buildForm function, inside the "FormController". Just adding 'hidden' when you add the field is enough.

public function buildForm(FormBuilder $builder, array $options)
{
    $builder->add('name');
    $builder->add('email', 'email');
    $builder->add('subject');
    $builder->add('anyone', 'hidden');
}