Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render only one field of a form with symfony

Tags:

twig

symfony

I want to render only one field of a form. When i put {{form_end(form)}} every other field are coming (symfony doc show it clearly) but how to render only one field ? If i dont put {{form_end(form)}}, there is only one field, but no save button

thanks

like image 927
ramsey_lewis Avatar asked Feb 09 '23 09:02

ramsey_lewis


1 Answers

Yes, CSS can do the trick. But do you want the working of your application to depend on client side styling rules? In most cases it might beter not to render the field HTML at all.

There are two ways in which you can fix this in your template.

  1. Put {% do form.field_you_want_to_hide.setRendered %} before your {{form_end(form)}}. This will mark the field as rendered and thus it will not show up when form_rest is called.

  2. Instead of {{form_end(form)}}, use {{ form_end(form, {'render_rest': false}) }}, as explained in the Symfony Twig documentation.

It would be even better to change your form class such that the fields are removed from your form. Is it your own form you would like to render, or a form from a third party bundle?

like image 105
Xatoo Avatar answered Feb 12 '23 04:02

Xatoo