在Symfony2.1中手动更改或捕获无效CSRF令牌的消息 [英] Manually change or catch the message for invalid CSRF token in Symfony2.1

查看:82
本文介绍了在Symfony2.1中手动更改或捕获无效CSRF令牌的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Symfony2.1
它具有针对表单的内置 CSRF保护。 CSRF令牌无效时返回的错误消息是: CSRF令牌无效。请尝试重新提交表单



I使用经典调用将其显示在Twig模板的表单顶部:

  {{form_errors(form)}} 

如何更改返回的消息?



<另一种方法是,更高级的可能性是捕获此错误类型,以便在我的Twig模板中显示很多选项/链接。
有任何想法吗?

解决方案

您尝试在文件 validators中设置吗?{locale_code } .yml 为密钥设置转换CSRF令牌无效。请尝试重新提交表格吗?
要更改默认消息,您可以尝试以下方法:

 #MyBundle\资源\翻译\验证器.en.yml 

CSRF令牌无效。请尝试重新提交以下表单:我的自定义CSRF错误

抛出错误消息此处,它属于FormError类,因此应该是可以翻译。


I'm using Symfony2.1. It has a builtin CSRF protection for the forms. The error message returned when the CSRF token is invalid is: "The CSRF token is invalid. Please try to resubmit the form".

I show it on the top of the form in my Twig template by using the classic call:

{{ form_errors(form) }}  

How can I change the returned message?

In alternative, a more advanced possibility is to catch this error type in order to show a lot of options/links in my Twig template. Any idea?

解决方案

Did you try to set in the file validators.{locale_code}.yml to set a translation for key The CSRF token is invalid. Please try to resubmit the form? To change the default message you can try this out:

 #MyBundle\Resources\translations\validators.en.yml

 The CSRF token is invalid. Please try to resubmit the form : My custom CSRF error

The error message is thrown here and it is of FormError class...thus it should be possible to translate it.

这篇关于在Symfony2.1中手动更改或捕获无效CSRF令牌的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆