Welcome Guest, Not a member yet? Register   Sign In
start and end date validation for searches [SOLVED]
#1

[eluser]tim1965[/eluser]
Hi

I am struggling with a couple of issues around form validation. This is my first website. I have a search form that allows a user to specify a start and end date for searching using jquery datepicker in the format dd/mm/yyyy. So my first problem is how to ensure that the end date is set before allowing the search to proceed using form validation and also whether i need to validate the date beyond just xss clean and trim. I could probably do this using Jquery but want to ensure it is also done in PHP using CI´s form validation library. I am guessing i need some form of callback but am absoloutely stumped as to how to do this.

My second issue is kind of tied around the first. I have a third field that is dependant on the start and end date parameters being set. I hide this and only display on input to start and end date using jquery, but once again want to make sure i have this covered in CI as well.
Thanks in advance for any help.
#2

[eluser]bretticus[/eluser]
[quote author="tim1965" date="1257861727"] I am guessing i need some form of callback but am absoloutely stumped as to how to do this.[/quote]

Stumped on how to call the callback or how to validate the dates?

To call it, you just add it to your validation rules for the field you want to validate:

Code:
$this->form_validation->set_rules('date1', 'Ending Date', 'required|callback__datechecker');
//I use two underscores between callback and the name of the callback function (_datechecker)
// because I want my callback function to be prefixed with one underscore because
// that makes it inaccessible to browsing.

Somewhere in your same controller class, build a _datecheck() method:

Code:
function _datechecker($date) //the argument is the value of the field specified in the rule above...
{
   // set the error message for this callback rule (http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#settingerrors)

   // if valid date return true otherwise return false.
}

For getting the date components you do something similar to extract the date parts and then use the checkdate function to validate:

Code:
list($month, $day, $year) = explode('/', $date1);



[quote author="tim1965" date="1257861727"]I have a third field that is dependant on the start and end date parameters being set. I hide this and only display on input to start and end date using jquery, but once again want to make sure i have this covered in CI as well.[/quote]

I think that is pretty much nothing more than sticking a "required" in your validation rule.

See form validation in the manual.
#3

[eluser]tim1965[/eluser]
Bretticus

Thanks for the reply.

I will give this a go once i have finished fixing my cookie problem in DX_auth.
#4

[eluser]tim1965[/eluser]
Sorry i have just re-read my original post and i did not make clear probably the most important point.
These fields are optional , they only become required if they are filled in. i.e. end_date is only required if start_date is filled in, likewise start_date is mandatory if end_date is entered, likewise the third field is optional until filled in in which case i need to check that start and end date are completed to properly run the query. If no data is entered into any fields then i dont need to apply the check.
Apologies i should have made clear, my fault for trying to do two things at once.
#5

[eluser]bretticus[/eluser]
You can modify your date checker method to look for the presence of another field variable:

Code:
// check date callback snippet for end_date
if ( $this->input->post('start_date') !== false ) {
   //validate date and return true or false
} else {
   return TRUE;
}
#6

[eluser]tim1965[/eluser]
Bretticus

Many thanks for your help i managed to get the callback working and created a couple of functions. They arent pretty but they work for me. To avoid some pain for someone in the future i will post them here.
These are for start and end date searches on a form so both fields are optional and the checks only kick in if either are filled in.
Controller
Code:
$this->form_validation->set_rules('start_date','Start Date','callback__datechecker_startdate');
         $this->form_validation->set_rules('end_date','End Date','callback__datechecker_enddate');
checker for startdate
allows for start date to be blank and only checks for the presence of end date if start date contains data
Code:
function _datechecker_startdate($date)
{
$start_date= $this->input->post('start_date');
$end_date=$this->input->post('end_date');

if ( $this->input->post('start_date') == "" )
{
return TRUE;
}
if ((empty($end_date)) && (!empty($start_date)))
    {
      $this->form_validation->set_message('_datechecker_startdate', 'You must enter an End date, if you want to search on Date availability');
    return FALSE;
    }
    else
    {
      list($day, $month, $year) = explode('/', $date);

        if (checkdate($month, $day, $year))
             {
              return TRUE;
             }
             ELSE
             {
              $this->form_validation->set_message('_datechecker_startdate', 'The %s you entered is not a valid date, it should be in the format dd/mm/yyyy');
                    return FALSE;
             }
    }

}//end function
checker for enddate
allows for end date to be blank and only checks for the presence of end date if start date contains data
Code:
function _datechecker_enddate($date)
{
$start_date= $this->input->post('start_date');
$end_date=$this->input->post('end_date');

if ($this->input->post('end_date') == "")
{
  return TRUE;
}
if ((empty($start_date)) && (!empty($end_date)))
    {
      $this->form_validation->set_message('_datechecker_enddate', 'You must enter a Start date, if you want to search on Date availability');
    return FALSE;
    }
    else
    {
      list($day, $month, $year) = explode('/', $date);

        if (checkdate($month, $day, $year))
             {
              return TRUE;
             }
             ELSE
             {
              $this->form_validation->set_message('_datechecker_enddate', 'The %s you entered is not a valid date, it should be in the format dd/mm/yyyy');
                    return FALSE;
             }
    }

}//end function
#7

[eluser]crwtrue[/eluser]
There is no validation if end_date is after start_date. So i would add this to the datechecker_enddate-function.

After if (checkdate($month, $day, $year)) line use strtotime-function to make dates in to unix timestamp like this:

$start_date = strtotime($start_date);
$end_date = strtotime($end_date);

and then check if end_date is after start_date like this:

Code:
if ($start_date < $end_date)
             {
              return TRUE;
             }
             ELSE
             {
              $this->form_validation->set_message('_datechecker_enddate', 'end_date must be after start_date');
                    return FALSE;
             }

Also i noticed that when you are calling the datechecker_enddate-function you are allready passing the end_date-value to datechecker_enddate-function but you are also reading both start_date and end_date to variables using post-function and you are using both $date and $end_date-variables which has the same value.




Theme © iAndrew 2016 - Forum software by © MyBB