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

Skip to content

Add block prefix to csrf token field #29862

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2019
Merged

Conversation

alexander-schranz
Copy link
Contributor

@alexander-schranz alexander-schranz commented Jan 12, 2019

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:

{%- 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:

{%- block token_widget %}
    {{ render_esi(controller('SuluFormBundle:FormWebsite:token', { 'form': form.parent.vars.name })) }}
{%- endblock token_widget -%}

@@ -90,9 +90,10 @@ public function finishView(FormView $view, FormInterface $form, array $options)
$tokenId = $options['csrf_token_id'] ?: ($form->getName() ?: \get_class($form->getConfig()->getType()->getInnerType()));
$data = (string) $options['csrf_token_manager']->getToken($tokenId);

$csrfForm = $factory->createNamed($options['csrf_field_name'], 'Symfony\Component\Form\Extension\Core\Type\HiddenType', $data, array(
$csrfForm = $factory->createNamed($options['csrf_field_name'], 'Symfony\Component\Form\Extension\Core\Type\HiddenType', $data, [
'block_prefix' => 'token',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

token -> csrf_token ?

@javiereguiluz
Copy link
Member

This looks nice to me ... but I'd like to ask @HeahDude and @vudaltsov if they can imagine any problem for adding this option unconditionally and for "hardcoding" its value. Thanks!

Copy link
Contributor

@HeahDude HeahDude left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really simple and nice addition, thank you very much!

@fabpot
Copy link
Member

fabpot commented Jan 16, 2019

Thank you @alexander-schranz.

@fabpot fabpot merged commit 02bd689 into symfony:master Jan 16, 2019
fabpot added a commit that referenced this pull request Jan 16, 2019
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
@alexander-schranz alexander-schranz deleted the patch-2 branch January 16, 2019 10:19
@fabpot fabpot mentioned this pull request May 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants