symfony2 CSRF 无效 [英] symfony2 CSRF invalid

查看:29
本文介绍了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天全站免登陆