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!

图片转代码服务由CSDN问答提供 功能建议

我正在开发一个Web应用程序。 我有应该从网上创建和编辑的实体(相当经典,不是它)。

所以我想使用表单生成(使用Type和Handler)Symfony 提供,但我不想简单地以标准方式打印它们。 我想从jquery中调用这些表单,以便我可以在jQuery插件中使用验证等,例如 jEditable

因此,我认为最好的方法是将我的表单作为json发送到jquery函数,该函数将把它放在我的HTML元素中。 \ n

我做了什么:

jQuery:

  $。get(“getorganisationform”  /“+ curOId,function(data){
 if(data.responseCode == 200){
 $(”#innerModal“)。append(data.form); 
} 
}); 
 <  / code>  
 
 

控制器:

  $ form = $ this-&gt; createForm(new OrganisationType,  $ organization); 
 
 $ formHandler = new OrganisationHandler($ form,$ this-&gt; get('request'),$ this-&gt; getDoctrine() - &gt; getEntityManager()); 
 
 if  ($ formHandler-&gt; process())
 {
返回$ this-&gt;重定向($ this-&gt; generateUrl('adminpanel')); 
} 
 
返回新响应(json_encode($  form),200); 
   
 
 

json_encode()实际上不发送任何内容,因为$ form是一个对象。 现在我有几个问题:

  • 有没有办法获取表单的html元素?
  • 是否有必要使用 json?
  • 有没有更好的方法在jquery中重用表单生成? (我想在这里讨价还价)

    提前致谢!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题