2016-05-26 68 views
0

我試圖處理表單和一些額外的數據與AJAX發送。Symfony2處理jQuery序列化形式和額外的數據與AJAX後發送

這裏是我的ajax郵編:

$(document).on('submit', '#edit-entreprise', function (e) { 
     e.preventDefault(); 
     var $entreprise = $("#liste-entreprises").val(); 
     var $url = Routing.generate('load_edit_entreprise_form'); 
     var $formSerialize = $('#edit-entreprise').serialize() + "&entreprise=" + $entreprise; 
     $(".panel-entreprise").empty().append('<div class="progress"> <div class="indeterminate"></div> </div>'); 
     $.ajax({ 
      url: $url, 
      type: 'POST', 
      data: $formSerialize, 
      success: function(html) { 
       console.log(html); 
      } 
     }); 
    }); 

在這裏,我CONTROLER:

public function editAction(Request $request) 
    { 
     $entreprise = $request->request->get('entreprise'); 
     $entreprise = $this->getDoctrine() 
      ->getRepository('AvisClientsBundle:Entreprise') 
      ->find($entreprise); 

     $editForm = $this->createForm('AvisClientsBundle\Form\EntrepriseType', $entreprise); 
     $editForm->handleRequest($request); 

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

      return new Response(json_encode(array('status' => 'success'))); 
     } 
     return new Response(json_encode($this->render('AvisClientsBundle:Admin/Entreprise:edit.html.twig', array(
      'entreprise' => $entreprise, 
      'edit_form' => $editForm->createView(), 
     ))->getContent())); 
    } 

我不知道如何接收形式和額外的數據:(

你能解釋一下怎麼做嗎?

謝謝!

回答

0

我不喜歡這樣寫道:

阿賈克斯(我用FormData):

$('#formId').submit(function (event) { 
     // Eviter le comportement par défaut (soumettre le formulaire) 
     event.preventDefault(); 
     var $this = $(this); 

     $.ajax({ 
      url: $this.attr('action'), 
      type: $this.attr('method'), 
      data: new FormData($this[0]), 
      processData: false, 
      contentType: false, 
      error: function (request, status, error) { 
       callback(request.responseText); 
      }, 
      complete: function() { 
       // 
      }, 
      statusCode: { 
       //traitement en cas de succès 
       200: function (response) { 
        var message = response.message; 
        callback(response, event); 
       }, 
       412: function (response, event) { 
        callback(response); 
       } 
      } 
     }); 
    }); 

在SF2這裏是處理:

public function updateAction(Request $request, $id) { 
     $em = $this->getDoctrine()->getManager(); 
     $entity = $em->getRepository('testBundle:testEntity')->find($id); 
     if (!$entity) { 
      throw $this->createNotFoundException('Unable to find entity.'); 
     } 
     $editForm = $this->createEditForm($entity); 
     $editForm->handleRequest($request); 

     if ($editForm->isValid()) { 
      $em->flush(); 
      //envoi des données JSON en front 
      $response = new JsonResponse(); 
      $response->setStatusCode(200); 
      //ajout de données éventuelles 
      $response->setData(array(
       'message' => "Ligne buffer updated", 
       'form' => json_encode($this->getHtmlForm($entity)))); 
      return $response; 
     } else { 
      //form non valide 
      //envoi des données d'erreurs JSON en front 
      $response = new JsonResponse(); 
      $response->setStatusCode(412); 
      $response->setData(array(
       'form' => json_encode($this->getHtmlForm($entity)), 
       'message' => $editForm->getErrorsAsString(), 
      )); 
      return $response; 
     } 
    } 
+0

您好,非常感謝您的回答。我知道如何發佈與Ajax發送的表單。但是我需要將額外的數據分享給我的控制器。我想要的是讀取作爲我的對象的id的額外數據,並從我的窗體讀取來自我的對象的數據的數據。 – laurentmichel

相關問題