2014-07-05 82 views
0

我正在使用bootbox來渲染一個帶有symfony 2的表單。所以當我想動態地改變內容時我遇到了一個麻煩,我不知道如何。來自PHP的動態內容引導模式對話框Symfony2

所以這是我想

  1. 我點擊一個按鈕,從控制器呈現形式的模態對話框
 <button class="btn btn-primary btn-new" data-toggle="modal" data-target="#agregarRonda"> 
      Add My Entity 
     </button> 

     <div style="display: none;" class="modal fade" id="agregarRonda" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
      <div class="modal-dialog"> 
      <div class="modal-content"> 
        <div class="modal-header"> 
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
        <h4 class="modal-title" id="myModalLabel">Add my Entity</h4> 
        </div> 
        <div class="modal-body"> 
         {% embed "projete:MyEntity:newMyEntity.html.twig" %} 
         {% endembed %} 

        </div> 
      </div> 
      </div> 
     </div> 

2.當內部embebed我呈現形式newMyentity.html .twig這有一個按鈕,重定向到這種方法在我的控制器在symfony裏面像:

public function createMyEntityAction(Request $request) 
{ 
    $user = $this->get('security.context')->getToken()->getUser(); 
    $entity = new MyEntity(); 
    $form = $this->createMyEntityForm($entity); 
    $form->handleRequest($request); 
    if ($form->isValid()) 
    { 
     if(ifNotExist($entity->getDescription()) ) 
     { 
      //Do the right things 
     } 
     else{ 
      /* 
      * Return content to the modal dialog and don't hide modal dialog? 
      */ 
     } 
    } 
} 

因此,我調用ifNotExist方法來檢查一些事情。如果返回false,我希望將內容發送到模態對話框而不隱藏模式對話框並修改內容。

我該怎麼辦?

謝謝。

回答

1

你可以做那樣的事情。

public function createMyEntityAction(Request $request) 
{ 
    $user = $this->get('security.context')->getToken()->getUser(); 
    $entity = new MyEntity(); 
    $form = $this->createMyEntityForm($entity); 
    if ($request->getMethod()=="POST"){ 
     $form->handleRequest($request); 
     if ($form->isValid()) 
     { 
      $em = $this->getDoctrine()->getManager(); 
      $em->persist($entity); 
      $em->flush(); 

      return new Response($entity->getId(),201); 
     } 
    } 

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

你的實體:

use Symfony\Component\Validator\Constraints as Assert; 
... 
/** 
* MyEntity 
* @ORM\Entity() 
* @ORM\Table() 
*/ 
class MyEntity 
{ 
    ... 
    /** 
    * 
    * @Assert\NotBlank(message ="Plz enter the description") 
    */ 
    private $description; 
    ... 
} 

您的JS:

$('#yourAddBtnId').on('click', fcuntion(){ 
    var $modal = $('#yourModalId') 
    $.get("yourURL", null, function(data) { 
     $modal.find('modal-body').html(data); 
    }) 
    // create the modal and bind the button 
    $('#yourModalId').dialog({ 
     buttons: { 
     success: { 
      label: "Save", 
      className: "btn-success", 
      callback: function() { 
      that = this; 
      var data = {}; 
      // get the data from your form 
      $(that).find('input, textarea').each(function(){ 
       data[$(this).attr('name')] = $(this).val(); 
      }) 
      // Post the data 
      $.post("yourURL", function(data , status) { 
       if ("201" === status){ 
        $modal.modal('hide'); 
       } 
       else { 
        $modal.find('modal-body').html(data); 
       }  
      }); 
      } 
     } 
    }); 
}); 
+0

我沒有,你把這裏是相同的,但是是一種靈感,得到我想要的東西,我忘記告訴你:D –