2016-11-29 38 views
0

目前我在我的index.html.twig文件中有一个新的操作。Symfony:ajax表单加载

 <div id="formcontent"> 
      form content goes here 
     </div> 
     <a href="{{ path('uni_new') }}">Create a new uni</a> 

和我的新的行动看起来像这样

/** 
* Creates a new uni entity. 
* 
* @Route("/new", name="uni_new") 
* @Method({"GET", "POST"}) 
*/ 
public function newAction(Request $request) 
{ 
    $uni = new Uni(); 
    $form = $this->createForm('AppBundle\Form\UniType', $uni); 
    $form->handleRequest($request); 

    if ($form->isSubmitted() && $form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($uni); 
     $em->flush($uni); 

     return $this->redirectToRoute('uni_show', array('id' => $uni->getId())); 
    } 

    return $this->render('uni/new.html.twig', array(
     'uni' => $uni, 
     'form' => $form->createView(), 
    )); 
} 

我想通过Ajax请求来完成这个新动作。所以,当用户点击Create a new uni,而不是去一个新的网址,我希望通过ajax调用加载表单,并替换上面的div的内容,编号为formcontent。我怎样才能做到这一点?提前致谢。

+0

您显示的PHP代码总体上是OK的,您需要的是以JS为中心的,并且与Symfony无关。你应该在那里添加你的JS代码。 – lsouza

回答

0

对于ajax调用,所有Symfony controller可以做的仅仅是以格式HTML json格式响应格式HTML。

/** 
* Creates a new uni entity. 
* 
* @Route("/new", name="uni_new") 
* @Method({"GET", "POST"}) 
*/ 
public function newAction(Request $request) 
{ 
    $uni = new Uni(); 
    $form = $this->createForm('AppBundle\Form\UniType', $uni); 
    $form->handleRequest($request); 

    if ($form->isSubmitted() && $form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($uni); 
     $em->flush($uni); 

     /** 
     * If your form also need a ajax submit, respond output accordingly. 
     */ 

     return $this->redirectToRoute('uni_show', array('id' => $uni->getId())); 
    } 

    if ($request->isXmlHttpRequest()) { 
     $formHtml = $this->renderView('uni/new.html.twig', array(
      'uni' => $uni, 
      'form' => $form->createView(), 
     )); 
     return new JsonResponse(
      array(
       'success' => true, 
       'formHtml' => $formHtml 
      ) 
     ); 
    } 

    return $this->render('uni/new.html.twig', array(
     'uni' => $uni, 
     'form' => $form->createView(), 
    )); 
} 

使用$request->isXmlHttpRequest()如果它能够探测到一个Ajax调用,并作出相应的反应,该jQuery.ajax应注意休息的形式置换到div的条款。

注意:代码未经测试。

希望它有帮助!

0
$(function() { 
(function submitHandler() { 
    var $form = $("#your_form_id"); 
    $form.on('submit', function (e) { 
     e.preventDefault(); 
     $.ajax({ 
      type: $(this).attr('method'), 
      url: $(this).attr('action'), 
      data: $(this).serialize() 
     }) 
      .done(function (data) { 
       $form.replaceWith(data); 
       submitHandler(); 
      }) 
      .fail(function (jqXHR) { 
       if(jqXHR.status === 302) { 
        location.href = jqXHR.responseJSON; 
       } else { 
        location.href = Routing.generate('registration_fail') 
       } 
      }) 
    }) 
})(); 

});

你必须在你的前端添加一些js。在这段代码中,我使用了JQUERY。

在你的控制,当你的表格是有效的

if ($request->isXmlHttpRequest()) { 
       return new JsonResponse($this->generateUrl('registration_success'), 302); 
      } 

一些额外的代码你必须记住,重定向在控制器不工作的你,当你使用Ajax请求,所以你必须给客户重定向前端