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

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

问题描述

我有一个不断收到 CSRF令牌无效。请尝试重新提交该表单的表格。我一直找不到原因。

I have a form that keeps getting the "The CSRF token is invalid. Please try to resubmit the form." I have been unable to find the cause.

我知道这很简单。任何帮助将不胜感激。

I know it is something simple. any help would be appreciated.

在控制器中,我有这样的东西:

In the controller I have this:

public function settingsAction()
{   
    $request = $this->getRequest();

    /* @var $user User */
        $user = $this->get('security.context')->getToken();

    $settingsForm = $this->createForm(new SettingsFormType(), array($user));
    $billingForm = $this->createForm(new BillingFormType(), array($user));
    $accountForm = $this->createForm(new AccountFormType(), array($user));



        return $this->render('DeductrDeductrBundle:Settings:settings.html.twig', array(
                'settingsForm' => $settingsForm->createview(),'billingForm' => $billingForm->createview(),'accountForm' =>$accountForm->createview(),));
}

public function createAction()
{
    $em = $this->getDoctrine()->getEntityManager();

    $form = $this->createForm(new RegistrationFormType('Deductr\UserBundle\Entity\User'));

    $form->bind($this->getRequest());

    if ($form->isValid()) {
        $user = $form->getData();

        $em->persist($user);
        $em->flush();

        return $this->redirect('/settings');
    }

.twig文件具有以下内容:

The .twig file has this:

{% extends 'DeductrDeductrBundle:Base:base.html.twig' %}


{% block body %}
<div name='personal' style="padding-left:25px; width:600px;">
<h5>Personal Information</h5>
{#src/Deductr/DeductrBundle/Resources/views/settings/settings.html.twig #}
<form action="{{ path('user_registration_create') }}" {{ form_enctype(settingsForm) }}" method="POST" {{ form_enctype(settingsForm) }}>

 <table>
        <tr>
            <td>First Name</td>

            <td>Last Name</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>
        {{ form_widget(settingsForm.first_name, {'attr': {'class' : 'half'}}) }}
            </td>

            <td>
                {{ form_widget(settingsForm.last_name, {'attr':    {'class' : 'half'}}) }}
            </td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td colspan="3">Company</td>
        </tr>
        <tr>
            <td colspan="3">
                {{ form_widget(settingsForm.company, {'attr': {'class' : 'half'}}) }}

            </td>
        </tr>
        <tr>
            <td colspan="3">Address 1</td>
        </tr>
        <tr>
            <td colspan="3">
                {{ form_widget(settingsForm.address1, {'attr': {'class' : 'half'}}) }}

            </td>
        </tr>
        <tr>
            <td colspan="3">Address 2</td>
        </tr>
        <tr>
            <td colspan="3">
                {{ form_widget(settingsForm.address2, {'attr': {'class' : 'half'}}) }}

            </td>
            </tr>
            <tr>
            <td colspan='2'>
                <label for 'city'>City</label>
            </td>
            <td>&nbsp;</td>

            </tr>

        <tr>
            <td colspan='2'>
                {{ form_widget(settingsForm.city, {'attr': {'class': 'half'}}) }}
            </td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td><label for 'state'>State</label>
            <td>
                <label for 'zip'>Zip/Postal</label>
            </td>
            <td>
                <label for 'phone'>Phone Number</label>
            </td>

        </tr>
            <tr>
            <td>
                {{ form_widget(settingsForm.province, {'attr': {'class': 'half'}}) }}
            </td>
            <td>
                {{ form_widget(settingsForm.postal_code, {'attr': {'class': 'half'}}) }}
            </td>
            <td>
                {{ form_widget(settingsForm.phone, {'attr': {'class': 'half'}}) }}
            </td>

        </tr>
        <tr>
    <td>&nbsp;</td>         
    <td>&nbsp;</td>
    <td><input type="submit" value="{{ 'submit'|trans({}, 'DeductrUserBundle') }}" /></td>
</tr>
 </table> 
 {{ form_row (settingsForm._token) }} 
</form>

</div>
<div id="billing" style="padding-left:25px; width:600px;">
<h5>Billing Information</h5>
<form action="" method="POST" {{ form_enctype(billingForm) }}>
    {{ form_row (billingForm._token) }}

 <table>
    <!--    <tr>
            <td>First Name</td>

            <td>Last Name</td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td>
        {{ form_widget(billingForm.fname, {'attr': {'class' : 'half'}}) }}
            </td>

            <td>
                {{ form_widget(billingForm.lname, {'attr': {'class' : 'half'}}) }}
            </td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td colspan="3">Address 1</td>
        </tr>
        <tr>
            <td colspan="3">
                {{ form_widget(billingForm.billingAddress1, {'attr': {'class' : 'half'}}) }}

            </td>
        </tr>
        <tr>
            <td colspan="3">Address 2</td>
        </tr>
        <tr>
            <td colspan="3">
                {{ form_widget(billingForm.billingAddress2, {'attr': {'class' : 'half'}}) }}

            </td>
            </tr>
            <tr>
            <td colspan='2'>
                <label for 'city'>City</label>
            </td>
            <td>&nbsp;</td>

            </tr>

        <tr>
            <td colspan='2'>
                {{ form_widget(billingForm.billingCity, {'attr': {'class': 'half'}}) }}
            </td>
            <td>&nbsp;</td>
        </tr>
        <tr>
            <td><label for 'state'>State</label>
            <td>
                <label for 'zip'>Zip/Postal</label>
            </td>
            <td>
                <label for 'phone'>Phone Number</label>
            </td>

        </tr>
            <tr>
            <td>
                {{ form_widget(billingForm.billingState, {'attr': {'class': 'half'}}) }}
            </td>
            <td>
                {{ form_widget(billingForm.billingZip, {'attr': {'class': 'half'}}) }}
            </td>
            <td>
                {{ form_widget(billingForm.billingPhone, {'attr': {'class': 'half'}}) }}
            </td>

        </tr> -->
<tr>
    <td colspan='3'>
     <h5>Payment Information</h5>
    </td>
</tr>
    <tr>
        <td>Expiration Date</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
        <tr>
        <td>{{ form_widget(billingForm.expMonth, {'attr': {'class': 'half'}}) }}</td>
        <td>{{ form_widget(billingForm.expYear, {'attr': {'class': 'half'}}) }}</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
    </tr>
    <tr>
        <td>Credit Card Type</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
        <tr>
        <td>{{ form_widget(billingForm.ccType, {'attr': {'class': 'half'}}) }}</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
    </tr>
    <tr>
        <td>Credit Card Number</td>
        <td>&nbsp;</td>
        <td>CVV</td>
    </tr>
        <tr>
        <td>{{ form_widget(billingForm.cardNumber, {'attr': {'class': 'half'}}) }}</td>
        <td>&nbsp;</td>
        <td>{{ form_widget(billingForm.cvv, {'attr': {'class': 'half'}}) }}</td>
    </tr>
    <tr>
    </tr>
<tr>
    <td>&nbsp;</td>         
    <td>&nbsp;</td>
    <td><input type="submit" value="{{ 'submit'|trans({}, 'DeductrUserBundle') }}" /></td>

</tr>
</table>
</div>
</form>

</div>
<div id="account" style="padding-left:25px; width:600px;">
<h5>Account Information</h5>
<form action="" method="POST" {{ form_enctype(accountForm) }}>
    {{ form_row (accountForm._token) }}
<table>
    <tr>
        <td colspan='2'>Email</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td colspan='3'>{{ form_widget(accountForm.email, {'attr': {'class': 'half'}}) }}</td>
    </tr>
    <tr>
        <td colspan='2'>Password</td>
        <td>Confirm Your Password</td>
    </tr>
    <tr>
        <td>{{ form_widget(accountForm.password, {'attr': {'class': 'half'}}) }}</td>
        <td>&nbsp;</td>
        <td>{{ form_widget(accountForm.confPass, {'attr': {'class': 'half'}}) }}</td>
    </tr>
    <tr>
    <td colspan='2'>&nbsp;</td>
    <td><input type="submit" value="{{ 'submit'|trans({}, 'DeductrUserBundle') }}" /></td>

</tr>
</table>
 </form>
</div>
{% endblock %}


推荐答案

如果是您的整个表单模板,可能是这样:

If this is your entire form template, it's probably this:

<form action="{{ path('user_registration_create') }}" {{ form_enctype(settingsForm) }}" method="POST" {{ form_enctype(settingsForm) }}>
    {{ form_row (settingsForm._token) }}

确保包含 form_rest(settingsForm)

但是,为什么要在一个页面上放置三个表单呢?如果您是Symfony的新手,请尝试首先在一个页面上使用一个表单,您将-在您的情况下,需要三个 form_rest s-我认为每种实例每个实例一个。

But also, why three forms on one page? If you are new to Symfony, try getting one form to work on a page first. You would - in your case, need three form_rests - one per instance of each form I think.

很难说出没有更多的代码tbh。

Hard to tell without more code tbh.

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

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