在cakepp控制器出现故障后,重定向到模态(自动打开自举模态)

redirect to the modal ( automatic open bootstrap modal) after failing condition in cakephp controller

本文关键字:模态 重定向 控制器 cakepp 故障      更新时间:2023-09-26

这里的问题是,如果用户没有填写地址,则通过自动打开引导模式重定向用户以填写模式地址

这是我的控制器代码

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>    

尝试以下方法。

在您的控制器中:

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')

是的,我得到了答案,我在我的ctp cakefp视图中编写了脚本

<script>
$('#submit').click(function(e){
   if($.trim($('#address_line_1').val()) == ''){
        $("#myModal").modal("show");
        e.preventDefault();
       }
});
</script>

模型中上面的文本框id是address_line_1,我必须检查它是否为空。