简体   繁体   中英

How to deal with dynamic forms with symfony?

I want to send a form via ajax, but how print then?

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

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

Then I get the form via ajax:

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

My html file:

....
<div id="form"></div>

But it doesn't work. How can I print this form?

Normaly I use: {{ form(form) }} form print this.

Just create template with one line {{ form(form) }} and then render it:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM