2015-12-23 4 views
1

Проблемы здесь, если пользователь не заполнил адрес перенаправление пользователя, чтобы заполнить модальный адрес, автоматически открыв Bootstrap режимныхперенаправлять модальный (автоматическую открытой самозагрузки модальным) после провала состояния в контроллере CakePHP

Вот мой контроллер код

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>  
+0

что я попробовал здесь [просто ознакомьтесь с этой ссылкой] (http://stackoverflow.com/questions/6323649/cakephp-can-i-redirect-to-a-specific-div-of-a-view -после-успех) – Fronto

ответ

0

Попробуйте следующий подход.

В контроллере:

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.

0

Да, я получил ответ, я написал сценарий в моей CTP зрения CakePHP

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

выше текстового поля ID в модели address_line_1 что я должен проверить пустой или нет.

Смежные вопросы