我有一个表单不断收到“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> </td>
</tr>
<tr>
<td>
{{ form_widget(settingsForm.first_name, {'attr': {'class' : 'half'}}) }}
</td>
<td>
{{ form_widget(settingsForm.last_name, {'attr': {'class' : 'half'}}) }}
</td>
<td> </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> </td>
</tr>
<tr>
<td colspan='2'>
{{ form_widget(settingsForm.city, {'attr': {'class': 'half'}}) }}
</td>
<td> </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> </td>
<td> </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> </td>
</tr>
<tr>
<td>
{{ form_widget(billingForm.fname, {'attr': {'class' : 'half'}}) }}
</td>
<td>
{{ form_widget(billingForm.lname, {'attr': {'class' : 'half'}}) }}
</td>
<td> </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> </td>
</tr>
<tr>
<td colspan='2'>
{{ form_widget(billingForm.billingCity, {'attr': {'class': 'half'}}) }}
</td>
<td> </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> </td>
<td> </td>
</tr>
<tr>
<td>{{ form_widget(billingForm.expMonth, {'attr': {'class': 'half'}}) }}</td>
<td>{{ form_widget(billingForm.expYear, {'attr': {'class': 'half'}}) }}</td>
<td> </td>
</tr>
<tr>
</tr>
<tr>
<td>Credit Card Type</td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>{{ form_widget(billingForm.ccType, {'attr': {'class': 'half'}}) }}</td>
<td> </td>
<td> </td>
</tr>
<tr>
</tr>
<tr>
<td>Credit Card Number</td>
<td> </td>
<td>CVV</td>
</tr>
<tr>
<td>{{ form_widget(billingForm.cardNumber, {'attr': {'class': 'half'}}) }}</td>
<td> </td>
<td>{{ form_widget(billingForm.cvv, {'attr': {'class': 'half'}}) }}</td>
</tr>
<tr>
</tr>
<tr>
<td> </td>
<td> </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> </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> </td>
<td>{{ form_widget(accountForm.confPass, {'attr': {'class': 'half'}}) }}</td>
</tr>
<tr>
<td colspan='2'> </td>
<td><input type="submit" value="{{ 'submit'|trans({}, 'DeductrUserBundle') }}" /></td>
</tr>
</table>
</form>
</div>
{% endblock %}