19
19
use Symfony \Component \Form \FormInterface ;
20
20
use Symfony \Component \Form \FormView ;
21
21
use Symfony \Component \Form \Util \ServerParams ;
22
+ use Symfony \Component \OptionsResolver \Options ;
22
23
use Symfony \Component \OptionsResolver \OptionsResolver ;
23
24
use Symfony \Component \Security \Csrf \CsrfTokenManagerInterface ;
24
25
use Symfony \Contracts \Translation \TranslatorInterface ;
@@ -35,6 +36,8 @@ public function __construct(
35
36
private ?TranslatorInterface $ translator = null ,
36
37
private ?string $ translationDomain = null ,
37
38
private ?ServerParams $ serverParams = null ,
39
+ private array $ fieldAttr = [],
40
+ private ?string $ defaultTokenId = null ,
38
41
) {
39
42
}
40
43
@@ -73,6 +76,7 @@ public function finishView(FormView $view, FormInterface $form, array $options):
73
76
$ csrfForm = $ factory ->createNamed ($ options ['csrf_field_name ' ], HiddenType::class, $ data , [
74
77
'block_prefix ' => 'csrf_token ' ,
75
78
'mapped ' => false ,
79
+ 'attr ' => $ this ->fieldAttr + ['autocomplete ' => 'off ' ],
76
80
]);
77
81
78
82
$ view ->children [$ options ['csrf_field_name ' ]] = $ csrfForm ->createView ($ view );
@@ -81,13 +85,24 @@ public function finishView(FormView $view, FormInterface $form, array $options):
81
85
82
86
public function configureOptions (OptionsResolver $ resolver ): void
83
87
{
88
+ if ($ defaultTokenId = $ this ->defaultTokenId ) {
89
+ $ defaultTokenManager = $ this ->defaultTokenManager ;
90
+ $ defaultTokenId = static fn (Options $ options ) => $ options ['csrf_token_manager ' ] === $ defaultTokenManager ? $ defaultTokenId : null ;
91
+ }
92
+
84
93
$ resolver ->setDefaults ([
85
94
'csrf_protection ' => $ this ->defaultEnabled ,
86
95
'csrf_field_name ' => $ this ->defaultFieldName ,
87
96
'csrf_message ' => 'The CSRF token is invalid. Please try to resubmit the form. ' ,
88
97
'csrf_token_manager ' => $ this ->defaultTokenManager ,
89
- 'csrf_token_id ' => null ,
98
+ 'csrf_token_id ' => $ defaultTokenId ,
90
99
]);
100
+
101
+ $ resolver ->setAllowedTypes ('csrf_protection ' , 'bool ' );
102
+ $ resolver ->setAllowedTypes ('csrf_field_name ' , 'string ' );
103
+ $ resolver ->setAllowedTypes ('csrf_message ' , 'string ' );
104
+ $ resolver ->setAllowedTypes ('csrf_token_manager ' , CsrfTokenManagerInterface::class);
105
+ $ resolver ->setAllowedTypes ('csrf_token_id ' , ['null ' , 'string ' ]);
91
106
}
92
107
93
108
public static function getExtendedTypes (): iterable
0 commit comments