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 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示