@@ -37,7 +37,7 @@ public function __construct(
37
37
private ?string $ translationDomain = null ,
38
38
private ?ServerParams $ serverParams = null ,
39
39
private array $ fieldAttr = [],
40
- private ? string $ defaultTokenId = null ,
40
+ private string | array | null $ defaultTokenId = null ,
41
41
) {
42
42
}
43
43
@@ -50,11 +50,17 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
50
50
return ;
51
51
}
52
52
53
+ $ csrfTokenId = $ options ['csrf_token_id ' ]
54
+ ?: $ this ->defaultTokenId [$ builder ->getType ()->getInnerType ()::class]
55
+ ?? $ builder ->getName ()
56
+ ?: $ builder ->getType ()->getInnerType ()::class;
57
+ $ builder ->setAttribute ('csrf_token_id ' , $ csrfTokenId );
58
+
53
59
$ builder
54
60
->addEventSubscriber (new CsrfValidationListener (
55
61
$ options ['csrf_field_name ' ],
56
62
$ options ['csrf_token_manager ' ],
57
- $ options [ ' csrf_token_id ' ] ?: ( $ builder -> getName () ?: $ builder -> getType ()-> getInnerType ()::class) ,
63
+ $ csrfTokenId ,
58
64
$ options ['csrf_message ' ],
59
65
$ this ->translator ,
60
66
$ this ->translationDomain ,
@@ -70,7 +76,7 @@ public function finishView(FormView $view, FormInterface $form, array $options):
70
76
{
71
77
if ($ options ['csrf_protection ' ] && !$ view ->parent && $ options ['compound ' ]) {
72
78
$ factory = $ form ->getConfig ()->getFormFactory ();
73
- $ tokenId = $ options [ ' csrf_token_id ' ] ?: ( $ form ->getName () ?: $ form -> getConfig ()->getType ()-> getInnerType ()::class );
79
+ $ tokenId = $ form ->getConfig ()->getAttribute ( ' csrf_token_id ' );
74
80
$ data = (string ) $ options ['csrf_token_manager ' ]->getToken ($ tokenId );
75
81
76
82
$ csrfForm = $ factory ->createNamed ($ options ['csrf_field_name ' ], HiddenType::class, $ data , [
@@ -85,9 +91,11 @@ public function finishView(FormView $view, FormInterface $form, array $options):
85
91
86
92
public function configureOptions (OptionsResolver $ resolver ): void
87
93
{
88
- if ($ defaultTokenId = $ this ->defaultTokenId ) {
94
+ if (\is_string ( $ defaultTokenId = $ this ->defaultTokenId ) && $ defaultTokenId ) {
89
95
$ defaultTokenManager = $ this ->defaultTokenManager ;
90
96
$ defaultTokenId = static fn (Options $ options ) => $ options ['csrf_token_manager ' ] === $ defaultTokenManager ? $ defaultTokenId : null ;
97
+ } else {
98
+ $ defaultTokenId = null ;
91
99
}
92
100
93
101
$ resolver ->setDefaults ([
0 commit comments