@@ -37,7 +37,7 @@ public function __construct(
3737 private ?string $ translationDomain = null ,
3838 private ?ServerParams $ serverParams = null ,
3939 private array $ fieldAttr = [],
40- private ? string $ defaultTokenId = null ,
40+ private string | array | null $ defaultTokenId = null ,
4141 ) {
4242 }
4343
@@ -50,11 +50,17 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
5050 return ;
5151 }
5252
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+
5359 $ builder
5460 ->addEventSubscriber (new CsrfValidationListener (
5561 $ options ['csrf_field_name ' ],
5662 $ options ['csrf_token_manager ' ],
57- $ options [ ' csrf_token_id ' ] ?: ( $ builder -> getName () ?: $ builder -> getType ()-> getInnerType ()::class) ,
63+ $ csrfTokenId ,
5864 $ options ['csrf_message ' ],
5965 $ this ->translator ,
6066 $ this ->translationDomain ,
@@ -70,7 +76,7 @@ public function finishView(FormView $view, FormInterface $form, array $options):
7076 {
7177 if ($ options ['csrf_protection ' ] && !$ view ->parent && $ options ['compound ' ]) {
7278 $ 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 ' );
7480 $ data = (string ) $ options ['csrf_token_manager ' ]->getToken ($ tokenId );
7581
7682 $ csrfForm = $ factory ->createNamed ($ options ['csrf_field_name ' ], HiddenType::class, $ data , [
@@ -85,9 +91,11 @@ public function finishView(FormView $view, FormInterface $form, array $options):
8591
8692 public function configureOptions (OptionsResolver $ resolver ): void
8793 {
88- if ($ defaultTokenId = $ this ->defaultTokenId ) {
94+ if (\is_string ( $ defaultTokenId = $ this ->defaultTokenId ) && $ defaultTokenId ) {
8995 $ defaultTokenManager = $ this ->defaultTokenManager ;
9096 $ defaultTokenId = static fn (Options $ options ) => $ options ['csrf_token_manager ' ] === $ defaultTokenManager ? $ defaultTokenId : null ;
97+ } else {
98+ $ defaultTokenId = null ;
9199 }
92100
93101 $ resolver ->setDefaults ([
0 commit comments