3
votes

I have two Date fields to collect data from users. Need to validate it using codeigniter form validation class.

Scenario :

  1. First date field can be null

  2. Second date field cannot be null

  3. First date field should not be greater then todays date

  4. Second date field should be greater than first date field

    $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').']');

and the callback function is:

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;
              }
          }
2
sorry it is not so clear. what have you tried so far ? what errors did you find ?aberna
I was not able to formate the code. if I press and click on ({}) button OR (Ctrl+k) then its not formating well. :(Mukesh S

2 Answers

3
votes

if you are taking proper date just convert it with strtotime then comparison will be easy

here is modified function

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

In /application/controllers/Welcome controller I added a handler function and set the validations rules and custom callbacks (and define a custom route to this controller function $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;
    }
}

The view file to this controller: Optionally I loaded Boostrap because I formatted messages with 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>