为什么 $form['_csrf_token']->render();在 symfony 1.4.8 中渲染没有值的隐藏输入?

发布于 2024-10-01 06:06:34 字数 814 浏览 1 评论 0原文

我使用 symfony 1.4.8,当尝试渲染隐藏的 csrf 表单字段时,值不会添加到渲染的字段中。我以前做过这个,没有问题。请参阅下面的示例以查看代码和渲染的输出。

代码:

<?php echo $form['_csrf_token']->render(); ?>

生成:

<input type="hidden" name="contact[_csrf_token]" id="contact__csrf_token" />

代码:

<?php echo $form['_csrf_token']->renderRow(); ?>

生成:

<tr>
  <th><label for="contact__csrf_token"> csrf token</label></th>
  <td><input type="hidden" name="contact[_csrf_token]" value="3cf960d4553e2649f86d0ccd12a26efe" id="contact__csrf_token" /></td>
</tr>

如您所见,第二种方法生成 csrf_token 的值,但它还生成所有其他行信息。 render() 方法应该只生成带有值的“小部件”(在本例中为隐藏的输入字段)。由于某种原因,它没有增加价值。

I using symfony 1.4.8 and when trying to render the hidden csrf form field a value is not being added to the rendered field. I've done this before without issue. See the following to examples below to see the code and the rendered output.

Code:

<?php echo $form['_csrf_token']->render(); ?>

Generates:

<input type="hidden" name="contact[_csrf_token]" id="contact__csrf_token" />

Code:

<?php echo $form['_csrf_token']->renderRow(); ?>

Generates:

<tr>
  <th><label for="contact__csrf_token"> csrf token</label></th>
  <td><input type="hidden" name="contact[_csrf_token]" value="3cf960d4553e2649f86d0ccd12a26efe" id="contact__csrf_token" /></td>
</tr>

As you can see the second method generates the value for the csrf_token, but it also generates all the other row information. The render() method is supposed to just generate the 'widget' (in this case the hidden input field) with the value. For some reason it does not add a value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦中楼上月下 2024-10-08 06:06:34

为什么需要显式渲染它?为什么不使用 $form->renderHiddenFields()

Why do you need to explicitly render it? Why are you not using $form->renderHiddenFields() ?

内心荒芜 2024-10-08 06:06:34

使用 $form->renderHiddenFields() 代替。

Use $form->renderHiddenFields() instead.

雨巷深深 2024-10-08 06:06:34

你必须进行硬刷新。您的会话中出现问题。

硬刷新是 Shift+F5

如果失败,请删除您的域的所有域 cookie,然后重试。

这只是一个会话问题,前提是您没有在其他地方弄乱 csrf。

You have to do a hard refresh. Something is stuck in your session.

Hard refresh is Shift+F5

If that fails, delete all your domain cookies for your domain and try again.

It is simply a session issue, provided you've not messed with csrf elsewhere.

冷心人i 2024-10-08 06:06:34

尽管 symfony 中出现此行为有许多不同的原因:

  • 在处理表单验证之前不要使用 bind
  • 会话时间已结束,并且没有会话 ID。
  • CSRF 验证已禁用或未在 settings.yml 文件中设置 csrf_secret

我提出了一个快速解决方案来直接打印 CSRF 令牌值:

<input type="hidden" name="signin[_csrf_token]" id="signin__csrf_token" value="<?php echo $form->getCSRFToken(); ?>" />

使用:

$form->getCSRFToken();

将渲染一个新的生成的 CSRF 令牌。

Although there are some many different reasons for this behavior in symfony:

  • Do not use bind before process form validation.
  • Session time is over and there is no session id.
  • CSRF Validation is disabled or have not being set a csrf_secret value in settings.yml file

I present a quick solution to get CSRF Token value directly printed:

<input type="hidden" name="signin[_csrf_token]" id="signin__csrf_token" value="<?php echo $form->getCSRFToken(); ?>" />

Using:

$form->getCSRFToken();

will render a new generated CSRF Token.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文