Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 38247dd

Browse files
committed
feature #29862 Add block prefix to csrf token field (alexander-schranz)
This PR was squashed before being merged into the 4.3-dev branch (closes #29862). Discussion ---------- Add block prefix to csrf token field | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #... | License | MIT | Doc PR | symfony/symfony-docs#10867 Currently I use the following code snippet to overwrite the token rendering: ```twig {%- block hidden_widget -%} {%- if form.vars.name == '_token' -%} {{ block('app__token_widget') }} {%- else -%} {{ block('hidden_widget', 'form_div_layout.html.twig') }} {%- endif -%} {%- endblock hidden_widget -%} {%- block app__token_widget %} {{ render_esi(controller('SuluFormBundle:FormWebsite:token', { 'form': form.parent.vars.name })) }} {%- endblock app__token_widget -%} ``` With the change of https://symfony.com/blog/new-in-symfony-4-3-simpler-form-theming this workaround can now be removed and the following can be used: ```twig {%- block token_widget %} {{ render_esi(controller('SuluFormBundle:FormWebsite:token', { 'form': form.parent.vars.name })) }} {%- endblock token_widget -%} ``` Commits ------- 02bd689 Add block prefix to csrf token field
2 parents 18fb7f8 + 02bd689 commit 38247dd

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/Symfony/Component/Form/Extension/Csrf/Type/FormTypeCsrfExtension.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,10 @@ public function finishView(FormView $view, FormInterface $form, array $options)
9090
$tokenId = $options['csrf_token_id'] ?: ($form->getName() ?: \get_class($form->getConfig()->getType()->getInnerType()));
9191
$data = (string) $options['csrf_token_manager']->getToken($tokenId);
9292

93-
$csrfForm = $factory->createNamed($options['csrf_field_name'], 'Symfony\Component\Form\Extension\Core\Type\HiddenType', $data, array(
93+
$csrfForm = $factory->createNamed($options['csrf_field_name'], 'Symfony\Component\Form\Extension\Core\Type\HiddenType', $data, [
94+
'block_prefix' => 'csrf_token',
9495
'mapped' => false,
95-
));
96+
]);
9697

9798
$view->children[$options['csrf_field_name']] = $csrfForm->createView($view);
9899
}
@@ -103,20 +104,20 @@ public function finishView(FormView $view, FormInterface $form, array $options)
103104
*/
104105
public function configureOptions(OptionsResolver $resolver)
105106
{
106-
$resolver->setDefaults(array(
107+
$resolver->setDefaults([
107108
'csrf_protection' => $this->defaultEnabled,
108109
'csrf_field_name' => $this->defaultFieldName,
109110
'csrf_message' => 'The CSRF token is invalid. Please try to resubmit the form.',
110111
'csrf_token_manager' => $this->defaultTokenManager,
111112
'csrf_token_id' => null,
112-
));
113+
]);
113114
}
114115

115116
/**
116117
* {@inheritdoc}
117118
*/
118119
public static function getExtendedTypes(): iterable
119120
{
120-
return array(FormType::class);
121+
return [FormType::class];
121122
}
122123
}

0 commit comments

Comments
 (0)