javascript - cakephp Controller 中的失败条件后重定向到模式(自动打开 Bootstrap 模式)

标签 javascript jquery twitter-bootstrap cakephp cakephp-2.x

这里的问题是,如果用户未填写地址,则会重定向用户通过自动打开 Bootstrap 模态来填写模态地址

这是我的 Controller 代码

if ($this->request->is('post')) {

        ['address_line_1']);
        if($this->request->data['PropManagementAddress']['address_line_1'] == null){
        $this->redirect(array('#myModal'));
        //$this->Session->setFlash(__('Add Address by pressing add button below'));
        }else {

        $this->PropManagementUser->create();
        if ($this->PropManagementUser->saveAll($this->request->data)) {
            $this->Session->setFlash(__('The user has been saved.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            //prd($this->PropManagementUser->validationErrors);
            $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
        }
    }

这是我的add.ctp代码

<label>Add Address </label>
<button type="button" class="form-group" style="margin-left:140px;"data-toggle="modal" data-target="#myModal">Add</button>
<div class="modal fade" id="myModal" role="dialog">
    <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">addrress</h4>
            </div>
            <div class="modal-body">
                <?php
                echo $this->Form->input('PropManagementAddress.address_line_1',array('div' => array('class'=>'form-group'),'placeholder' => 'address line 1','label' => 'Address Line 1','class' => 'form-control'));
                echo $this->Form->input('PropManagementAddress.address_line_2',array('div' => array('class'=>'form-group'),'placeholder' => 'address line 2','label' => 'Address Line 2','class' => 'form-control'));
                echo $this->Form->input('PropManagementAddress.city_id',array('div' => array('class'=>'form-group'),'type'=>'select','options'=>$cities,'placeholder' =>'city id','label' => 'City ID','class' => 'form-control'));
                echo $this->Form->input('PropManagementAddress.state_id',array('div' => array('class'=>'form-group'),'type'=>'select','options'=>$states,'label' => 'State ID','class' => 'form-control'));
                echo $this->Form->input('PropManagementAddress.country_id',array('div' => array('class'=>'form-group'),'placeholder' =>'country id','label' => 'Country ID','class' => 'form-control'));
                echo $this->Form->input('PropManagementAddress.pincode',array('div' => array('class'=>'form-group'),'type'=>'text','placeholder' => 'pincode','label' => 'Pincode','class' => 'form-control'));
                echo $this->Form->input('PropManagementAddress.longitude',array('div' => array('class'=>'form-group'),'type'=>'text','placeholder' => 'longitude','label' => 'Longitude','class' => 'form-control'));
                echo $this->Form->input('PropManagementAddress.latitude',array('div' => array('class'=>'form-group'),'type'=>'text','placeholder' => 'latitude','label' => 'Latitude','class' => 'form-control'));
                ?>

                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Save</button>
                </div>
            </div>    
        </div>
    </div>    

最佳答案

尝试以下方法。

在你的 Controller 中:

if ($this->PropManagementUser->saveAll($this->request->data)) {
     $this->Session->setFlash(__('The user has been saved.'));
     return $this->redirect(array('action' => 'index'));
 } else {
     $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
     $this->set('showModal',true); 
 }

您认为:

<?php if (!empty($showModal)):?>
    <?php $this->Html->scriptStart(array('inline'=>false))?>

        $('#myModal').modal('show');

    <?php $this->Html->scriptEnd()?>
<?php endif ?>

参见.modal('show')在 Bootstrap 文档中。

关于javascript - cakephp Controller 中的失败条件后重定向到模式(自动打开 Bootstrap 模式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34436470/

相关文章:

css - 自定义网格系统

css - 如何在 Bootstrap 3.3.6 中定义轮播大小

javascript - 三.JS : object appears after zoom

带有简单换行符的 jQuery 克隆

javascript - 在其他地方单击时菜单应该消失

javascript - jsTree:如何在刷新后选择节点

twitter-bootstrap - Bootstrap中表格行的下拉菜单

javascript - jQuery 可排序不适用于多个容器

c# - ASP .net 从 page_load 函数中的代码隐藏值

javascript - 获取该月的第几周