Explicitly print CSRF token field instead of form_rest(form)?

Polmonino picture Polmonino · Mar 28, 2012 · Viewed 11.6k times · Source

How can I explicitly print CSRF field instead of using {{ form_rest(form) }}?

I need this because I'm going to hide/show some fields based on conditions, however {{ form_rest(form) }} is going to print all of the remaining fields (which is what I'd like to avoid).

Answer

Elnur Abdurrakhimov picture Elnur Abdurrakhimov · Mar 28, 2012

It can be done this way:

{{ form_widget(form._token) }}

Also you might want to consider adding fields conditionally in your form type instead of making that kind of decisions in a template.