What are the Slack Archives?

It’s a history of our time together in the Slack Community! There’s a ton of knowledge in here, so feel free to search through the archives for a possible answer to your question.

Because this space is not active, you won’t be able to create a new post or comment here. If you have a question or want to start a discussion about something, head over to our categories and pick one to post in! You can always refer back to a post from Slack Archives if needed; just copy the link to use it as a reference..

Hey! I am looking for a possibility to set a custom translated "invalid csrf token" message for all

UPPB2H525
UPPB2H525 Posts: 51 πŸ§‘πŸ»β€πŸš€ - Cadet
edited October 2020 in Help

Hey! I am looking for a possibility to set a custom translated "invalid csrf token" message for all the forms used in our shop.
Since it is symfony/form as underlying package it is possible to set this in general in the corresponding configureOptions method as

$resolver->setDefaults([
    'csrf_message' => 'some message',
]);

but this way i have to set this for each and every form. Is there a way to set this on a more abstract level?

Comments

  • Unknown
    edited October 2020

    did you try the glossary key form.csrf.error.text in data/import/common/common/glossary.csv (for me line 3564)?

  • UPPB2H525
    UPPB2H525 Posts: 51 πŸ§‘πŸ»β€πŸš€ - Cadet

    Uff may be i thought too complicated, will try this! Thx!

  • UPPB2H525
    UPPB2H525 Posts: 51 πŸ§‘πŸ»β€πŸš€ - Cadet

    Hey Marco! Unfortunally thats not so easy. the message form.csrf.error.text is only used in some single places but not as a general translation for the symfony message.

  • UPPB2H525
    UPPB2H525 Posts: 51 πŸ§‘πŸ»β€πŸš€ - Cadet

    Also it is not only used in case of the token was invalid or absent (as it is designed to be in symfony by setting it in the $resolver->setDefaults(...) method) but in every case of !$form->isValid() what can have other reasons. I tested this in current spryker-b2b-demo shop.

  • UPPB2H525
    UPPB2H525 Posts: 51 πŸ§‘πŸ»β€πŸš€ - Cadet
    edited October 2020

    Ok i found a solution. For people who are interested in:
    β€’ create a new Pyz\Yves\Form\Plugin\Provider\CsrfExtensionServiceProvider and register it in YvesBootstrap
    β€’ set $app['form.extension.csrf'] with own CsrfExtension-Class from project level in CsrfExtensionServiceProvider:

    $app['form.extension.csrf'] = $app->share(function ($app) {
     if (isset($app['translator'])) {
      return new CsrfExtension($app['form.csrf_provider'], $app['translator']); 
     }
    
     return new CsrfExtension($app['form.csrf_provider']);
    });
    

    β€’ in CsrfExtension load own FormTypeCsrfExtension from project level
    β€’ in FormTypeCsrfExtension set csrf_message to your needs in configureOptions method. This also can be a translation key of your glossary
    Not a very beautiful way of resolving this problem, but the best i found.