我有一个表单输入日期,其中开始日期和结束日期可能与数据库中的现有数据不同。 所以这里是我的代码:
<script>
.........
........
start: {
required: true,
date: true,
remote: {
url: "<?php echo site_url('activity/check_date')?>",
type: "post",
data: {
start: function(){ return $("#date").val(); }
}
}
},
end: {
required: true,
date: true,
remote: {
url: "<?php echo site_url('activity/check_end')?>",
type: "post",
data: {
end: function(){ return $("#end").val(); }
}
}
},
},
messages: {
start:
{
remote: 'Date already in use.'
},
end:
{
remote: 'Date already in use.'
},
},
........
........
</script>
&#13;
<input type="text" class="form-control" id="date" name="start" >
<input type="text" class="form-control" readonly id="end" name="end" required>
&#13;
我的模特:
public function tanggal($start)
{
$nik=$this->session->userdata('nik');
$this->db->where('start_date', $start);
$this->db->where('nik',$nik);
$query = $this->db->get('t_trx_activity');
if( $query->num_rows() > 0 )
{
return TRUE;
} else
{
return FALSE;
}
}
public function end_date($end)
{
$nik=$this->session->userdata('nik');
$this->db->where('end_date', $end);
$this->db->where('nik',$nik);
$query = $this->db->get('t_trx_activity');
if( $query->num_rows() > 0 )
{
return TRUE;
} else
{
return FALSE;
}
}
&#13;
我的控制员:
public function check_date()
{
$start=$this->input->post('start');
$result=$this->activity->tanggal($start);
if($result)
{
echo "false";
}else{
echo "true";
}
}
public function check_end()
{
$end=$this->input->post('end');
$result=$this->activity->end_date($end);
if($result)
{
echo "false";
}else{
echo "true";
}
}
&#13;
我认为我的功能在它之上并不是简单的代码,我想创建模型和控制器只有一个功能。因为start_date和结束日期在一个表中。 那么如何解决验证存在一个函数?
答案 0 :(得分:0)
使用单一路线将类型传递给模型:
模型
public function check_date_model($type,$val)
{
$nik=$this->session->userdata('nik');
$this->db->where($type, $val);
$this->db->where('nik',$nik);
$query = $this->db->get('t_trx_activity');
if( $query->num_rows() > 0 )
{
return TRUE;
} else
{
return FALSE;
}
}
控制器
public function check_date($type)
{
$start=$this->input->post('data');
$type=$this->input->post('type');
$result=$this->activity->check_date_model($type,$data);
if($result)
{
echo "false";
}else{
echo "true";
}
}
JS:
required: true,
date: true,
remote: {
url: "<?php echo site_url('activity/check_date')?>",
type: "post",
data: {
data:$("#date").val(),
type:"start_date"
}
}
},
end: {
required: true,
date: true,
remote: {
url: "<?php echo site_url('activity/check_date')?>",
type: "post",
data: {
data: $("#end").val(),
type:"end_date"
}
}
},