【问题标题】:Dynamically content bootstrap modal dialog from PHP Symfony2来自 PHP Symfony2 的动态内容引导模式对话框
【发布时间】:2014-07-05 19:33:34
【问题描述】:

我正在使用 bootbox 使用 symfony 2 呈现表单。所以当我想动态更改内容时我遇到了一个麻烦,我不知道如何。

这就是我想要的

  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.当我渲染一个表单 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,我希望将内容发送到模态对话框而不隐藏模态对话框并修改内容。

我该怎么做?

谢谢。

【问题讨论】:

    标签: javascript php twitter-bootstrap symfony bootbox


    【解决方案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', function(){
        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);
                    }      
                });
              }
          }
        });
    });
    

    【讨论】:

    • 我和你放在这里的不一样,但我得到了我想要的东西的灵感,我忘了告诉你:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 2020-12-12
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多