How to render form_rest () as hidden fields in symfony2 / twig?

I have a Form class that contains many fields. I would like to make a few of them and convey the left 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> 
+4
source share
4 answers

You must do this in the buildForm function, inside the "FormController". Just adding “hidden” when adding a field is enough.

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

form_rest () displays all the raw fields from your form. It just displays them as they are, so if you want to display the remaining fields as “hidden”, you just need to define them as “hidden” in your form!

+2
source

In addition, you can set all unnecessary fields as branches displayed in the template:

 <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> {% do form.unneededfield1.setRendered %} {% do form.unneededfield2.setRendered %} {% do form.unneededfield3.setRendered %} <div display="hidden">{{ form_rest(form) }}</div> <input type="submit" /> </form> 
+2
source
 {{ form_end(form, {'render_rest': false}) }} 

This is from the official documentation (v3.0) , so I think this is a very good practice.

0
source

Source: https://habr.com/ru/post/1400350/


All Articles