首页 文章

Codeigniter:验证两个Date字段

提问于
浏览
3

我有两个Date字段来收集用户的数据 . 需要使用codeigniter表单验证类来验证它 .

场景:

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

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

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

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

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

1 回答

  • 3

    如果您正在使用正确的日期,只需将其转换为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;
                  }
              }
    

相关问题