@@ -72,6 +72,8 @@ protected forms. As an alternative, you can:
7272 load the CSRF token with an uncached AJAX request and replace the form
7373 field value with it.
7474
75+ .. _csrf-protection-forms :
76+
7577CSRF Protection in Symfony Forms
7678--------------------------------
7779
@@ -82,7 +84,54 @@ protected against CSRF attacks.
8284.. _form-csrf-customization :
8385
8486By default Symfony adds the CSRF token in a hidden field called ``_token ``, but
85- this can be customized on a form-by-form basis::
87+ this can be customized (1) globally for all forms and (2) on a form-by-form basis.
88+ Globally, you can configure it under the ``framework.form `` option:
89+
90+ .. configuration-block ::
91+
92+ .. code-block :: yaml
93+
94+ # config/packages/framework.yaml
95+ framework :
96+ # ...
97+ form :
98+ csrf_protection :
99+ enabled : true
100+ field_name : ' custom_token_name'
101+
102+ .. code-block :: xml
103+
104+ <!-- config/packages/framework.xml -->
105+ <?xml version =" 1.0" encoding =" UTF-8" ?>
106+ <container xmlns =" http://symfony.com/schema/dic/services"
107+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
108+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
109+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
110+ https://symfony.com/schema/dic/services/services-1.0.xsd
111+ http://symfony.com/schema/dic/symfony
112+ https://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
113+
114+ <framework : config >
115+ <framework : form >
116+ <framework : csrf-protection enabled =" true" field-name =" custom_token_name" />
117+ </framework : form >
118+ </framework : config >
119+ </container >
120+
121+ .. code-block :: php
122+
123+ // config/packages/framework.php
124+ use Symfony\Config\FrameworkConfig;
125+
126+ return static function (FrameworkConfig $framework) {
127+ $framework->form()->csrfProtection()
128+ ->enabled(true)
129+ ->fieldName('custom_token_name')
130+ ;
131+ };
132+
133+ On a form-by-form basis, you can configure the CSRF protection in the ``setDefaults() ``
134+ method of each form::
86135
87136 // src/Form/TaskType.php
88137 namespace App\Form;
0 commit comments