2011-11-30 49 views
3

我使用ajax调用表单,而不是在页面中显示它。Symfony2通过Ajax获取表单

一切都在工作,除了一件奇怪的事情。

当我通过ajax调用表单并将其添加到页面时,默认值不在字段中。

但是,如果我打电话通过正常的URL形式,默认值设置...

$builder 
     ->add('email', null,array('data' => 'default value') 
     ->add('type_form', null, array('data' => 'default value', 'property_path'=> false)) 
     ->add('list_choice', 'choice', array(
      'choices' => $options['list'], 
      'expanded'=>true, 
      'multiple'=>true, 
     )); 

领域的电子邮件和type_form不会显示在“默认值”的值,除非我通过打电话的形式他的网址,而不是通过ajax。

是否有通过ajax调用窗体的具体方法? 感谢您的回答。

在这里,我在我的控制器

$this->container->get('templating')->render('MyappSiteBundle:Contributions:filter_themes_form.html.twig', array(
'form'=> $form->createView(), 
'type'=> $type)); 

$response = new Response(json_encode(array('form'=> $response_form))); 


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

然后我打电话的形式创建表单的方式......

+0

您究竟如何用Ajax调用表单? –

+0

我使用jquery,我调用我的控制器动作的路由,它创建窗体并将其返回到Json var中。那么jquery会做一个innerHtml将表单添加到页面中。 – Rmannn

+0

我添加代码以提供帮助 – Rmannn

回答

2

我找到了解决办法。我被清除表单数据之前,发送:

if ($request->getMethod() == 'POST') { 
     $form->bindRequest($request); 
     if ($form->isValid()) { 
      $data = $form->getData(); 
      return $data; 
     } 
    } 

然后我打电话的形式。