如何使用symfony处理动态表单?

时间:2016-03-11 19:50:51

标签: php jquery ajax forms symfony

我想通过ajax发送一个表单,但是怎么打印呢?

@Bean
public MappedInterceptor myInterceptor()
{
    return new MappedInterceptor(null, new MyInterceptor());
}

然后我通过ajax获取表单:

ajaxControllerAction(){
$form = $this->createFormBuilder()
                ->add('date', 'date')
                ->add('text', 'text')
                ->getForm();
}

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

我的html文件:

$.ajax({
            type: "POST",
            url: Routing.generate('ajaxController', {
            }),
            dataType: "json",
            beforeSend: function () {
            },
            success: function (data) {
                $('#form).html(data.form);
            }
        });

但它不起作用。我该如何打印此表格?

Normaly我使用:{{form(form)}}形式打印这个。

1 个答案:

答案 0 :(得分:1)

只需使用一行{{ form(form) }}创建模板,然后呈现它:

return new JsonResponse([
  'form' => $this->renderView('.../yourView.html.twig', compact('form'))
]);