CSRF 令牌无效.请尝试重新提交表格 [英] The CSRF token is invalid. Please try to resubmit the form

查看:25
本文介绍了CSRF 令牌无效.请尝试重新提交表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每次尝试提交表单时都会收到此错误消息:

<块引用>

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

我的表单代码是这样的:

<form novalidate action="{{path('signup_index')}}" method="post" {{form_enctype(form)}} role="form" class="form-horizo​​ntal"><div class="form-group">{{ form_label(form.email, 'Email', {'label_attr': {'class': 'col-md-1 control-label'}}) }}{{ form_widget(form.email, {'attr': {'class': 'col-md-2'}}) }}{{ form_errors(form.email) }}

<div class="form-group">{{ form_label(form.nickname, 'Nickname', {'label_attr': {'class': 'col-md-1 control-label'}}) }}{{ form_widget(form.nickname, {'attr':{'class': 'col-md-2'}}) }}{{ form_errors(form.nickname, {'attr': {'class': 'col-md-3'}}) }}

<div class="form-group">{{ form_label(form.password, 'password', {'label_attr': {'class': 'col-md-1 control-label'}}) }}{{ form_widget(form.password, {'attr': {'class': 'col-md-2'}}) }}{{ form_errors(form.password, {'attr': {'class': 'col-md-3'}}) }}

<div class="form-group">{{ form_label(form.password_repeat, '重复密码', {'label_attr': {'class': 'col-md-1 control-label'}}) }}{{ form_widget(form.password_repeat, {'attr':{'class': 'col-md-2'}}) }}{{ form_errors(form.password_repeat, {'attr': {'class': 'col-md-3'}}) }}

<div class="form-group"><div class="col-md-1 control-label"><输入类型=提交"值=提交">

</表单>

有什么想法吗?

解决方案

您需要在表单中添加 _token

{{ form_row(form._token) }}

截至目前,您的表单缺少 CSRF 令牌字段.如果您使用 twig 表单函数来呈现您的表单,例如 form(form) 这将自动为您呈现 CSRF 令牌字段,但是您的代码显示您正在使用像 这样的原始 HTML 呈现您的表单<form></form>,因此您必须手动呈现该字段.

或者,只需在表单的结束标记之前添加 {{ form_rest(form) }}.

<块引用>

根据文档

这将呈现所有尚未为给定呈现的字段形式.始终将它放在表单中的某个位置是个好主意因为它会为您呈现隐藏字段并制作您忘记的任何字段渲染更明显(因为它会为您渲染字段).

form_rest(view, variables)

I'm getting this error message every time I try to submit the form:

The CSRF token is invalid. Please try to resubmit the form

My form code is this:

<form novalidate action="{{path('signup_index')}}" method="post" {{form_enctype(form)}} role="form" class="form-horizontal">
    <div class="form-group">
        {{ form_label(form.email, 'Email', {'label_attr': {'class': 'col-md-1 control-label'}}) }}
        {{ form_widget(form.email, {'attr': {'class': 'col-md-2'}}) }}
        {{ form_errors(form.email) }}
    </div>

    <div class="form-group">
        {{ form_label(form.nickname, 'Nickname', {'label_attr': {'class': 'col-md-1 control-label'}}) }}
        {{ form_widget(form.nickname, {'attr':{'class': 'col-md-2'}}) }}
        {{ form_errors(form.nickname, {'attr': {'class': 'col-md-3'}}) }}
    </div>
    <div class="form-group">
        {{ form_label(form.password, 'password', {'label_attr': {'class': 'col-md-1 control-label'}}) }}
        {{ form_widget(form.password, {'attr': {'class': 'col-md-2'}}) }}
        {{ form_errors(form.password, {'attr': {'class': 'col-md-3'}}) }}
    </div>

    <div class="form-group">
        {{ form_label(form.password_repeat, 'Repeat password', {'label_attr': {'class': 'col-md-1 control-label'}}) }}
        {{ form_widget(form.password_repeat, {'attr':{'class': 'col-md-2'}}) }}
        {{ form_errors(form.password_repeat, {'attr': {'class': 'col-md-3'}}) }}
    </div>
    <div class="form-group">
        <div class="col-md-1 control-label">
        <input type="submit" value="submit">
    </div>

    </div>
</form>

Any ideas?

解决方案

You need to add the _token in your form i.e

{{ form_row(form._token) }}

As of now your form is missing the CSRF token field. If you use the twig form functions to render your form like form(form) this will automatically render the CSRF token field for you, but your code shows you are rendering your form with raw HTML like <form></form>, so you have to manually render the field.

Or, simply add {{ form_rest(form) }} before the closing tag of the form.

According to docs

This renders all fields that have not yet been rendered for the given form. It's a good idea to always have this somewhere inside your form as it'll render hidden fields for you and make any fields you forgot to render more obvious (since it'll render the field for you).

form_rest(view, variables)

这篇关于CSRF 令牌无效.请尝试重新提交表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆