2012-10-31 95 views
2

我有一個表格,我必須填寫一些信息。 即使我把裏面的東西,我得到的錯誤:Symfony2:CSRF令牌無效。請嘗試重新提交表格

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

與此相關的問題:symfony2 CSRF invalid我使用的正確形式 - $> bindRequest()

if ($this->getRequest()->getMethod() == 'POST') { 
    $form->bindRequest($this->getRequest()); 
    if ($form->isValid()) 
    { 
     ... 
    } 

這裏是我的模板(樹枝)代碼:

<div class="item item-last"> 
<h1>Create Affiliation</h1> 

{% if valid == false %} 
    <div class="error"> 
     {{ form_errors(form) }} 
     {{ form_errors(form.affiliation) }} 
     {{ error }} 
    </div> 
{% endif %} 
{% if app.session.flash('user-notice') != '' %} 
<div class="flash-notice"> 
    {% autoescape false %} 
    {{ app.session.flash('user-notice') }} 
    {% endautoescape %} 
</div> 
{% endif %} 

</div> 
<div class="item item-last"> 
<form action="{{ path('SciForumVersion2Bundle_user_submission_affiliation_create', {'hash_key' : submission.hashkey, 'author_id' : author.id }) }}?ajax=no" method="POST" class="authorForm" {{ form_enctype(form) }}> 
    <div style="float:left;"> 
     <table width="100%" cellspacing="0" cellpadding="0"> 
      <tr> 
       <td> 
        {{ form_label(form.affiliation) }} 
       </td> 
       <td> 
        {{ form_widget(form.affiliation, { 'attr': {'size': 40} }) }} 
       </td> 
      </tr> 
      <tr> 
       <td> 
        &nbsp; 
       </td> 
       <td> 
        <div class="button button-left button-cancel"> 
         <img src="{{ asset('bundles/sciforumversion2/images/design/new/button-red.png') }}"/> 
         <a href="{{ path('SciForumVersion2Bundle_user_submission_author_edit', { 'hash_key' : submission.hashkey, 'author_id' : 0 }) }}" class="submission_link">cancel</a> 
        </div> 
        <div style="float: left;">&nbsp;</div> 
        <div class="button button-left button-cancel"> 
         <img src="{{ asset('bundles/sciforumversion2/images/design/new/button.png') }}"/> 
         <input type="submit" name="login" value="submit" /> 
        </div> 
        <div style="clear: both;"></div> 
       </td> 
      </tr> 
     </table> 
    </div> 
{{ form_rest(form) }} 

</form> 
</div> 

這裏是js代碼:

function init_submission_functions() 
{ 

init_fck(); 

$(".submission_link").unbind("click").bind("click", function() { 

    var href = $(this).attr("href"); 
    if(href == null || href == '') return false; 

    $.ajax({ 
     type: "POST", 
     async: true, 
     url: href, 
     cache: false, 
     dataType: "json", 
     success: function(data) { 

      $("#content .contentwrap .itemwrap").html(data.content); 
      init_submission_functions(); 
     } 
    }); 

    return false; 
}); 

$(".authorForm").unbind("submit").bind("submit", function() { 

    var href = $(this).attr("action"); 
    if(href == null || href == '') return false; 

    var affiliation = "blabla"; 

    $.ajax({ 
     type: "POST", 
     async: true, 
     url: href, 
     affiliation: affiliation, 
     cache: false, 
     dataType: "json", 
     success: function(data) { 

      $("#content .contentwrap .itemwrap").html(data.content); 
      init_submission_functions(); 
     } 
    }); 

    return false; 
}); 
} 

但我仍然收到相同的錯誤。

+0

你能否粘貼你的模板? –

+0

@KristianZondervan,是的科西嘉,我會盡快編輯我的問題。 –

+0

@KristianZondervan,完成。也許我忘了提及我也使用Ajax作爲表單。 –

回答

8

使用serialize jQuery method發送序列化形式:

$form.submit(function (e) { 
    e.preventDefault(); 

    $this = $(this); 
    $.post($this.attr('action'), $this.serialize(), function (response) { 
     // handle the response here 
    }); 
}); 

這樣的Symfony將處理提交請求作爲一個正常的要求 - 你不必做任何特殊處理一個Ajax表單提交。你需要做的就是返回一個JsonResponse - 當然,如果你需要的話。

這裏是處理形式的例子 - 它適應您的需求:

if ('POST' === $request->getMethod()) { 
    $form->bind($request); 

    if ($form->isValid()) { 
     // do something here — like persisting or updating an entity 

     return new JsonResponse([ 
      'success' => true, 
     ]); 
    } 

    return new JsonResponse([ 
     'success' => false, 
     'form' => $this->render($pathToTheTemplateHere, [ 
      'form' => $form, 
     ], 
    ]); 
} 

另一種方法是使用不同的模板:form.json.twigform.html.twig - 閱讀文檔的詳細信息。

+0

謝謝@elnur,但是什麼是數據?我怎樣才能在這裏獲得數據?這是一個數組?謝謝。 –

+0

在Symfony方面,您可以像處理任何常用表單一樣處理表單。 –

+0

好的。請再提一個問題,如何返回JsonResponse? 「函數(響應)」的「響應」是jsonResponse? –

相關問題