Symfony2:在jquery中使用生成的表单

I'm working on a web application. I have entities that are supposed to be created and edited from the web (quite classic, isn't it).

So I want to use the form generation (with Type and Handler) Symfony offers, but I don't simply want to print them in the standard way. I'd like to call those forms from jquery, so that I can use validation and such in jQuery plugins such as jEditable.

Therefore, I suppose the best way to do this is to send my form as a json to the jquery function, which will put it in my HTML element.

What I've done :

jQuery:

$.get("getorganisationform/"+curOId, function(data){
    if(data.responseCode==200 ){   
        $( "#innerModal" ).append(data.form);
    }
});

Controller:

$form = $this->createForm(new OrganisationType, $organisation);

    $formHandler = new OrganisationHandler($form, $this->get('request'), $this->getDoctrine()->getEntityManager());

    if( $formHandler->process() )
    {
        return $this->redirect( $this->generateUrl('adminpanel') );
    }

    return new Response(json_encode($form), 200);

The json_encode() actually doesn't send anything, as $form is an object. Now I have several questions:

  • Is there a way to get the html elements of the form?
  • Is it necessary then to use json?
  • Is there a better way to reuse form generation in jquery? (I'm thinking about jeditable here)

Thanks in advance!

After some more research, I decided to render a twig that I fed my form. This way I get the html content sent in the response. Typically:

return $this->render('MyBundle:Admin:form.html.twig', array(
        'form' => $form->createView(),
    ));

Can you try this for getting html form content.

return new Response(
    json_encode(
        array(
            'form' => $form->createView(),
            'responseCode' => 200
            )
        ), 
    200
);