@@ -101,6 +101,8 @@ protected forms. As an alternative, you can:
101101 load the CSRF token with an uncached AJAX request and replace the form
102102 field value with it.
103103
104+ .. _csrf-protection-forms :
105+
104106CSRF Protection in Symfony Forms
105107--------------------------------
106108
@@ -111,7 +113,54 @@ o do anything to be protected against CSRF attacks.
111113.. _form-csrf-customization :
112114
113115By default Symfony adds the CSRF token in a hidden field called ``_token ``, but
114- this can be customized on a form-by-form basis::
116+ this can be customized (1) globally for all forms and (2) on a form-by-form basis.
117+ Globally, you can configure it under the ``framework.form `` option:
118+
119+ .. configuration-block ::
120+
121+ .. code-block :: yaml
122+
123+ # config/packages/framework.yaml
124+ framework :
125+ # ...
126+ form :
127+ csrf_protection :
128+ enabled : true
129+ field_name : ' custom_token_name'
130+
131+ .. code-block :: xml
132+
133+ <!-- config/packages/framework.xml -->
134+ <?xml version =" 1.0" encoding =" UTF-8" ?>
135+ <container xmlns =" http://symfony.com/schema/dic/services"
136+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
137+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
138+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
139+ https://symfony.com/schema/dic/services/services-1.0.xsd
140+ http://symfony.com/schema/dic/symfony
141+ https://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
142+
143+ <framework : config >
144+ <framework : form >
145+ <framework : csrf-protection enabled =" true" field-name =" custom_token_name" />
146+ </framework : form >
147+ </framework : config >
148+ </container >
149+
150+ .. code-block :: php
151+
152+ // config/packages/framework.php
153+ use Symfony\Config\FrameworkConfig;
154+
155+ return static function (FrameworkConfig $framework) {
156+ $framework->form()->csrfProtection()
157+ ->enabled(true)
158+ ->fieldName('custom_token_name')
159+ ;
160+ };
161+
162+ On a form-by-form basis, you can configure the CSRF protection in the ``setDefaults() ``
163+ method of each form::
115164
116165 // src/Form/TaskType.php
117166 namespace App\Form;
0 commit comments