Symfony 1.4:表单中CSRF的自定义错误消息

Tom*_*Tom 7 forms symfony1 csrf sfguard

任何人都可以告诉我在Symfony 1.4中为表单定制CSRF令牌错误消息的位置/方式.我正在使用sfDoctrineGuard进行登录,特别是在会话用完且你仍然打开页面时,它会抛出一个非常用户不友好的错误:"检测到CSRF攻击".类似"此会话已过期.请返回主页再试一次"听起来更好.

在表单类中执行此操作的正确方法是什么?

谢谢.

naa*_*aag 5

唯一的方法似乎是覆盖sfForm::addCSRFProtection().

/lib/form/BaseForm.class.php你可以添加这段代码:

class BaseForm extends sfFormSymfony
{
    public function addCSRFProtection($secret = null)
    {
        parent::addCSRFProtection($secret);
        if (array_key_exists(self::$CSRFFieldName, $this->getValidatorSchema())) {
            $this->getValidator(self::$CSRFFieldName)->setMessage('csrf_attack', 'This session has expired. Please return to the home page and try again.');
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在调用父方法之后,您将检索与CSRF字段关联的验证程序并更改代码的消息csrf_attack.

编辑:您还需要检查验证器是否存在.某些表单可能会禁用其CSRF保护!

希望这可以帮助!