那么如何解决验证存在一个函数



我有一个输入日期的表单,其中开始日期和结束日期可能与数据库中的现有数据不同。这里是我的代码:

<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>
<input type="text"  class="form-control"  id="date" name="start" >
<input type="text"  class="form-control" readonly id="end" name="end" required>

我的模型:

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; 
			}
	}

我的控制器:

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";
		}
	}

我认为我的功能在上面它不是简单的代码,我想创建模型和控制器只有一个功能。因为start_date和end date在一个表中。那么如何解决验证存在一个函数?

使用单一路由将类型传递给模型:

模型
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"
                                     }
                               }  
                    },

最新更新