How to render just the CSRF token in Symfony 2

forged-moniesIt’s not often – thankfully – that I need to customise a form to the extent where I can’t use the lovely {{ form_rest(myForm) }} functionality, but when that situation arises, and you still need the CSRF token, you may think you need to go into your form type and add it in there, in some weird hidden field.

The manual page is quite deceptive in that it helps perpetuate this viewpoint.

Thankfully the solution is very simple, simply put: {{ form_row(form._token) }} in your twig template and Symfony’s form creator and rendering processes will handle the rest.

I can’t take the credit for this one, so here’s my original source of knowledge.

Be Sociable, Share!

2 thoughts on “How to render just the CSRF token in Symfony 2”

  1. you can render a csrff token with

    but I dont know how symfony checks it, because if I change the token the form accepts the submit :/

Leave a Reply

Your email address will not be published. Required fields are marked *

This blog is kept spam free by WP-SpamFree.