这里的问题是,如果用户未填写地址,则通过自动打开Bootstrap模式重定向用户填充模态地址
这是我的控制器代码
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">×</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 :(得分: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 ?>
请参阅Bootstrap文档中的.modal('show')
。
答案 1 :(得分:0)
是的,我得到了答案,我在我的ctp cakephp视图中编写了脚本
<script>
$('#submit').click(function(e){
if($.trim($('#address_line_1').val()) == ''){
$("#myModal").modal("show");
e.preventDefault();
}
});
</script>
模型中的文本框ID是address_line_1,我必须检查是否为空。