dragonlew9876 2012-06-13 12:56
浏览 52
已采纳

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!

  • 写回答

2条回答

  • douduoting8408 2012-06-14 08:16
    关注

    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(),
        ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器