symfony2 CSRF无效 [英] symfony2 CSRF invalid

查看:76
本文介绍了symfony2 CSRF无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以今天我用实时数据库中的新信息更新了数据库...从那时起,我的其中一张表单就出现了问题。如果您需要任何代码,请告诉我,我将对其进行编辑并发布所需的代码...

Okay, so today I updated my database with new information from our 'live' database... And since then I've been having issues on one of my forms. If you need any code let me know and i'll edit this and post the code needed...

我有一个报告表单,其中包含一个日期范围字段和一个前往代理部门。当我第一次访问该页面时,我会在表格的开头看到以下内容:

I have a report form which has a date range field and a drop down for an agent department. When I first visit the page I see this at the beginning of the form:

CSRF令牌无效。请尝试重新提交表单

因此,我转到其他具有相同信息类型的表单,然后检查_token

So I go over to one of my other forms that has the same type of information, and check the _token out and this is what comes out:

<input type="hidden" id="ecs_crmbundle_TimeClockReportType__token" name="ecs_crmbundle_TimeClockReportType[_token]" value="87e358fbc4d6d3e83601216b907a02170f7bcd92" />
<input type="hidden" id="ecs_crmbundle_SimpleSalesReportType__token" name="ecs_crmbundle_SimpleSalesReportType[_token]" value="87e358fbc4d6d3e83601216b907a02170f7bcd92" />

第一个是显示错误的内容,而SimpleSalesReport却没有...任何想法为什么这样做或如何解决?

The first one is the one that shows the error, and the SimpleSalesReport does not... Any idea why this is doing this or how I can fix it?

谢谢。.

推荐答案

您是否偶然在产生CSRF错误的操作中使用$ form-> bindRequest()?我有这个问题。您不应该绑定新表格的请求。如果要将表单发布到同一操作,则将bindRequest包装在一个条件中,该条件检查方法是否为POST:

Are you by chance using $form->bindRequest() in the action which produces the CSRF error? I had this issue. You should not be binding the request for a new form. If you are posting the form to the same action, wrap the bindRequest in a conditional which checks if method is POST:

if ($this->getRequest()->getMethod() == 'POST') {
  $form->bindRequest($this->getRequest());
  if ($form->isValid()) {
    ...
  }
}

这篇关于symfony2 CSRF无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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