Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter:验证两个日期字段_Php_Forms_Validation_Codeigniter - Fatal编程技术网

Php Codeigniter:验证两个日期字段

Php Codeigniter:验证两个日期字段,php,forms,validation,codeigniter,Php,Forms,Validation,Codeigniter,我有两个日期字段来收集用户的数据。需要使用codeigniter表单验证类对其进行验证 情景: 第一个日期字段可以为空 第二个日期字段不能为空 第一个日期字段不应大于今天的日期 第二个日期字段应大于第一个日期字段 $this->form_validation->set_规则('first_field','first field','trim | required') $this->form_validation->set_rules('second_field','second fiel

我有两个日期字段来收集用户的数据。需要使用codeigniter表单验证类对其进行验证

情景:

  • 第一个日期字段可以为空

  • 第二个日期字段不能为空

  • 第一个日期字段不应大于今天的日期

  • 第二个日期字段应大于第一个日期字段

    $this->form_validation->set_规则('first_field','first field','trim | required')

  • $this->form_validation->set_rules('second_field','second field','trim | required | callback_check_equal_less['。$this->input->post('first_field'))

    回调函数是:

    function check_equal_less($second_field,$first_field)
              {
                if ($second_field <= $first_field)
                  {
                    $this->form_validation->set_message('check_equal_less', 'The First &amp;/or Second fields have errors.');
                    return false;       
                  }
                  else
                  {
                    return true;
                  }
              }
    
    函数检查等于小于($second\u字段,$first\u字段)
    {
    如果($second_field form_validation->set_message('check_equal_less','第一个&;/or第二个字段有错误');
    返回false;
    }
    其他的
    {
    返回true;
    }
    }
    
    如果您选择了正确的日期,只需将其与strotime进行转换,那么比较就很容易了

    这里是修改后的函数

    function check_equal_less($second_field,$first_field)
              {
                if (strtotoime($second_field) <= strtotime($first_field))
                  {
                    $this->form_validation->set_message('check_equal_less', 'The First &amp;/or Second fields have errors.');
                    return false;       
                  }
                  else
                  {
                    return true;
                  }
              }
    
    函数检查等于小于($second\u字段,$first\u字段)
    {
    如果(strtoime($second_field)表单_validation->set_message('check_equal_less'),则第一个和第二个字段有错误。');
    返回false;
    }
    其他的
    {
    返回true;
    }
    }
    
    /application/controllers/Welcome控制器中,我添加了一个处理函数,并设置验证规则和自定义回调(并定义此控制器函数的自定义路由
    $route['action\u page']='Welcome/dates\u validation';
    ):

    公共函数日期\u验证(){
    $this->load->helper(数组('form','url');
    $this->load->library('session');
    $this->load->library('form_validation');
    $this->form_validation->set_error_分隔符(“”,);
    $this->form_validation->set_rules('first_field','first date','trim | callback _check _equal_less');
    $this->form_validation->set_rules('second_field','second date','trim | required | callback_check _more_then['。$this->input->post('first_field'));
    $this->session->set_flashdata('result','');
    如果($this->form\u validation->run()==FALSE)
    {
    $this->session->set_flashdata('result',validation_errors());
    }
    否则{
    $this->session->set_flashdata('result','');
    }
    $this->load->view('forms');
    }
    公共功能检查等于或小于($date){
    $today=标准时间(日期(“Y-m-d”);
    $first_date=strottime($date);
    如果($date!=“”)和($first\u date>$today))
    {
    $this->form_validation->set_message('check_equal_less','第一个日期不能大于今天!');
    返回false;
    }
    其他的
    {
    返回true;
    }
    }
    公共功能检查(第二天、第一天){
    如果($first_date!=“”)和($second_date!=“”)和($strotime($first_date)>strotime($second_date)))
    {
    $this->form_validation->set_message('check_great_then','Second date field应该大于First date field!');
    返回false;
    }
    其他的
    {
    返回true;
    }
    }
    
    此控制器的视图文件: 可选地,我加载了Boostrap,因为我使用警报格式化了消息

    
    日期验证
    第一天:

    抱歉,不太清楚。到目前为止您尝试了什么?您发现了什么错误?我无法格式化代码。如果我按下并单击({})按钮或(Ctrl+k),则格式化不好:(
    public function dates_validation(){
        $this->load->helper(array('form', 'url'));
        $this->load->library('session');
    
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="alert alert-danger">', '</div>');
    
        $this->form_validation->set_rules('first_field', 'First date', 'trim|callback_check_equal_less');
        $this->form_validation->set_rules('second_field', 'Second date', 'trim|required|callback_check_greater_then['.$this->input->post('first_field').']');
    
        $this->session->set_flashdata('result', '');
    
        if ($this->form_validation->run() == FALSE)
        {
            $this->session->set_flashdata('result', validation_errors());
        }
        else{
            $this->session->set_flashdata('result', '');
        }
    
    
        $this->load->view('forms');
    }
    
    public function check_equal_less($date){
        $today = strtotime(date("Y-m-d"));
    
        $first_date = strtotime($date);
    
        if ( ($date != "") && ($first_date > $today) )
        {
            $this->form_validation->set_message('check_equal_less', 'The First date can not be greater than today!');
            return false;       
        }
        else
        {
            return true;
        }
    }
    
    public function check_greater_then($second_date, $first_date){
        
        if ( ($first_date != "") && ($second_date != "") && (strtotime($first_date) > strtotime($second_date)) )
        {
            $this->form_validation->set_message('check_greater_then', 'Second date field should be greater than First date field!');
            return false;       
        }
        else
        {
            return true;
        }
    }
    
    <div id="container">
        <h1>Date validations</h1>
    
        <?php echo $this->session->flashdata('result'); ?>
        <form action="/action_page" method="post">
          
          <label for="first_field">First date:</label><br>
          <input type="date" id="first_field" name="first_field" value="<?php echo set_value('first_field'); ?>"/></br>
          
          <label for="second_field">Second date:</label><br>
          <input type="date" id="second_field" name="second_field" value="<?php echo set_value('second_field'); ?>"/></br></br>
    
          <input type="submit" value="Submit">
        </form> 
    
    </div>