ramsey_lewis
ramsey_lewis

Reputation: 598

How to render only one field of a form with 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

Upvotes: 4

Views: 4123

Answers (1)

Sander Toonen
Sander Toonen

Reputation: 3563

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?

Upvotes: 8

Related Questions