【问题标题】:Codeigniter : Validating of two Date fieldsCodeigniter:验证两个日期字段
【发布时间】:2015-04-19 06:05:40
【问题描述】:

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

场景:

  1. 第一个日期字段可以为空

  2. 第二个日期字段不能为空

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

  4. 第二个日期字段应该大于第一个日期字段

    $this->form_validation->set_rules('first_field', 'First Field', 'trim|required');

$this->form_validation->set_rules('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;
              }
          }

【问题讨论】:

  • 抱歉,不太清楚。你都尝试了些什么 ?你发现了什么错误?
  • 我无法格式化代码。如果我按下并单击 ({}) 按钮或 (Ctrl+k),则其格式不正确。 :(

标签: php forms validation codeigniter


【解决方案1】:

如果您选择正确的日期,只需将其与 strtotime 进行转换,那么比较会很容易

这里是修改函数

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

【讨论】:

    【解决方案2】:

    /application/controllers/Welcome 控制器中,我添加了一个处理函数并设置了验证规则和自定义回调(并定义了一个自定义路由到此控制器函数$route['action_page'] = 'welcome/dates_validation';):

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

    这个控制器的视图文件: 可选地我加载了 Boostrap,因为我使用 alert

    格式化了消息
    <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>
    

    【讨论】:

      猜你喜欢
      • 2013-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-07
      • 2019-06-03
      • 1970-01-01
      • 2011-02-23
      • 2013-08-12
      相关资源
      最近更新 更多