1

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

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

在控制器中我有这个:

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 文件有这个:

{% 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 %}
4

3 回答 3

5

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

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

确保包括form_rest(settingsForm)

而且,为什么一页上有三个表格?如果您是 Symfony 的新手,请先尝试让一个表单在页面上工作。你会 - 在你的情况下,需要三个form_rests - 我认为每个表格的每个实例一个。

没有更多代码很难说。

于 2013-04-14T13:03:10.877 回答
0

我找到了答案。我没有添加这个:

/** 
 * Creates a new settungsUser entity. 
 * 
 * @Route("/settings/create", name="user_settings") 
 * @Method("POST") 
 * @Template("DeductrDeductrBundle:Settings:settings.html.twig")
 * 
 */ 

一旦我更新了树枝中的路径,它就起作用了。

感谢所有的帮助。

于 2013-04-15T16:45:34.577 回答
0

我认为您正在混合两种不同的表单类型。

您正在呈现 type 的形式SettingsFormType并将其提交给它,createAction从而创建了 type 的形式RegistrationFormType

生成 CSRF 令牌时可能会考虑表单名称。话虽如此,您的令牌每次都必然不同且不正确-无论如何;)

希望这对您的问题有所启发;)

于 2013-04-12T21:41:18.093 回答