Symfony2错误:CSRF令牌无效。请尝试重新提交表单

时间:2016-08-24 17:46:34

标签: php jquery ajax symfony twig

我一直在尝试提交一个以模态更新Cliente对象的表单。 当我尝试这样做时,显示以下错误消息: CSRF令牌无效。请尝试重新提交表单。

她是我的财务总监:

$NODE_DEBUG

模板editCliente

public function editClienteAction(Request $req,$id)
{
    $c=$this->getDoctrine()->getRepository("MFCBClienteBundle:Cliente")->find($id);
    $form=$this->createFormBuilder($c)
        ->add('nom','text')
        ->add('prenom','text')
        ->add('age','text')
        ->add('adresse','text')
        ->add('tel','text')
        ->add('email','text')
        ->add('save','submit')
        ->getForm();
    if ($this->getRequest()->getMethod() == 'POST') {
        $form->handleRequest($req);
        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($c);
            $em->flush();

            $response = new Response(json_encode([
                'success' => true,
            ]));

            $response->headers->set('Content-Type', 'application/json');
            return $response;

        }
    }
    return $this->render('MFCBClienteBundle:Default:editCliente.html.twig', array('id'=>$id,'form' => $form->createView()));

}

模板索引

<div class="modal-dialog modal-lg">
<div class="modal-content">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title" id="editModalLabel">Modifier cliente</h4>
    </div>
    <div class="modal-body">
        {{ form_start(form, {'attr': {'class': 'form-horizontal','id':'editForm'}}) }}
        {# Les erreurs générales du formulaire. #}
        {{ form_errors(form) }}
        <div class="form-group">
            {# Génération du label. #}
            {{ form_label(form.nom, "Nom : ", {'label_attr': {'class': 'col-sm-3 control-label'}}) }}
            {# Affichage des erreurs pour ce champ précis. #}
            {{ form_errors(form.nom) }}
            <div class="col-sm-4">
                {# Génération de l'input. #}
                {{ form_widget(form.nom, {'attr': {'class': 'form-control'}}) }}
            </div>
        </div>

        <div class="form-group">
            {{ form_label(form.prenom, "Prénom : ", {'label_attr': {'class': 'col-sm-3 control-label'}}) }}
            {{ form_errors(form.prenom) }}
            <div class="col-sm-4">
                {{ form_widget(form.prenom, {'attr': {'class': 'form-control'}}) }}
            </div>
        </div>

        <div class="form-group">
            {{ form_label(form.age, "Age : ", {'label_attr': {'class': 'col-sm-3 control-label'}}) }}
            {{ form_errors(form.age) }}
            <div class="col-sm-4">
                {{ form_widget(form.age, {'attr': {'class': 'form-control'}}) }}
            </div>
        </div>

        <div class="form-group">
            {{ form_label(form.tel, "Tèl : ", {'label_attr': {'class': 'col-sm-3 control-label'}}) }}
            {{ form_errors(form.tel) }}
            <div class="col-sm-4">
                {{ form_widget(form.tel, {'attr': {'class': 'form-control'}}) }}
            </div>
        </div>

        <div class="form-group">
            {{ form_label(form.adresse, "Adresse : ", {'label_attr': {'class': 'col-sm-3 control-label'}}) }}
            {{ form_errors(form.adresse) }}
            <div class="col-sm-4">
                {{ form_widget(form.adresse, {'attr': {'class': 'form-control'}}) }}
            </div>
        </div>

        <div class="form-group">
            {{ form_label(form.email, "Email : ", {'label_attr': {'class': 'col-sm-3 control-label'}}) }}
            {{ form_errors(form.email) }}
            <div class="col-sm-4">
                {{ form_widget(form.email, {'attr': {'class': 'form-control'}}) }}
            </div>
        </div>

    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Annuler</button>
        {# Pour le bouton, pas de label ni d'erreur, on affiche juste le widget #}
        {{ form_widget(form.save, {'attr': {'class': 'btn btn-primary'}}) }}
        {# Fermeture de la balise <form> du formulaire HTML #}
    </div>
    {{ form_rest(form) }}
    {{ form_end(form) }}
</div>

JS:

<table class="table table-striped ">
        <thead>
        <tr>
            <th>Nom</th><th>Prénom</th><th>Age</th><th>Adresse</th><th>Tel</th><th>Email</th><th>Opération</th>
        </tr>
        </thead>
        <tbody>
        {%for c in modal.listClientes %}
            <tr>
                <td>{{ c.nom }}</td><td>{{ c.prenom }}</td><td>{{ c.age }}</td><td>{{ c.adresse }}</td><td>{{ c.tel }}</td><td>{{ c.email }}</td>
                <td>
                    <div class="list-inline">
                        <a class="editLink btn" href="{{ path('editCliente', {'id': c.id}) }}" >
                            <i class="glyphicon glyphicon-pencil" aria-hidden="true"></i>&nbsp; Modifier</a>
                    </div>
                </td>
            </tr>
        {%endfor%}
        </tbody>
    </table>

<!-- editModal-->
<div id="editModal" class="modal fade edit-modal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">

</div>

2 个答案:

答案 0 :(得分:1)

在我的情况下{{ form_row(formOne._token) }} {{ form_row(formTwo._token) }} 解决问题。 如果您在同一个控制器中有多个表单,则可以根据需要调用多个表单。

mysql-connector-java 6.0.5

等等。

答案 1 :(得分:0)

检查你的html模板editClient:

<div class="modal-content">
    {{ form_start(form, ...) }} // right position!
    <div class="modal-body">
        {{ form_start(form, ...) }} // wrong position!
        ...
    </div>
    <div class="modal-footer">
        ...
        {{ form_widget(form.save, ...) }}
    </div>
    {{ form_rest(form) }}  
    {{ form_end(form) }}
</div>

form_restform_end函数必须位于form_start(即同一级别)的同一DOM元素容器中,否则form元素将呈现为空情况下。

因此,要发送的序列化数据表将为空,并且不会发送所需的表单令牌。检查这一点,在浏览器上检查您的代码。