2014-10-09 70 views
0

我試圖向我的控制器發送Ajax請求,在那裏獲取表單,如果某些數據爲真,需要刪除表單的某些部分,但我不知道如何才能獲取表單$請求對象。請幫幫我。如何在控制器動作中獲取表單Symfony 2

現在所有的代碼,我告訴有存在一定的方式,這樣做$request->getForm()

public funcion ajaxAction(Request $request) 
{ 
    if ($request->isXmlHttpRequest()) { 

     } 
} 
+0

您是否嘗試設置窗體行動'ajaxAction'?這樣你就可以繼續[docs](http://symfony.com/doc/current/book/forms.html#handling-form-submissions) – stevenll 2014-10-09 11:58:17

回答

1

你應該做這樣的事情(我假設你在你的控制器擴展Symfony\Bundle\FrameworkBundle\Controller\Controller是):

public funcion ajaxAction(Request $request) 
{ 
    if ($request->isXmlHttpRequest()) { 
     $form = $this->createForm(new YourFormType()); 
     $form->handleRequest($request); 
    } 
} 

之後,你有$form來自requst bound的數據。您可以撥打像isValid()getData()方法,此變量​​

檢查documentation更多信息

相關問題