2014-10-09 67 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更多信息

相关问题