• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to use form validation to compare date? - if not possible accept a parallel solution.

#1
[eluser]Spectruman[/eluser]
I have the following fields start date and end date start time end time. I need to do some checking to allow inclusion of data. Verifies that the start date is greater than the end date is also the same with the start time and end time and not allow inclusion of data if the start time is 12:00 and 14:00 hours late this time would be prohibited.

Code:
$this->form_validation->set_rules('dateStart','Date start','trim|required');
$this->form_validation->set_rules('dateEnd','Date endFim','trim|required');
$this->form_validation->set_rules('timeStart','Time start','trim|required');
$this->form_validation->set_rules('timeEnd','Time End','trim|required');

I would have this function to validate.
Code:
function compareDataTime($dateStart,$timeStart,$dateEnd,$timeEnd){
    if(($timeStart == '12:00' && $timeEnd == '14:00') || ($timeStart == '14:00' && $timeEnd == '12:00') ||                            
        ($timeStart == '12:00' && $timeEnd <= $timeStart) || ($timeStart >= '12:00' && $timeEnd <= $timeStart) ||            
        ($timeStart <= '12:00' && $timeEnd < $timeStart && $dateStart == $dateEnd) ||                            
        ($timeStart == $timeEnd && $dateStart == $dateEnd)){
        return false;
    }else{
        return true;
    }
}
Just do not know how I apply it using a custom validation. I appreciate any help welcome

#2
[eluser]Kamarg[/eluser]
Callback functions as explained in the user guide seem to be just what you're looking for.

#3
[eluser]Spectruman[/eluser]
[quote author="Kamarg" date="1271455255"]Callback functions as explained in the user guide seem to be just what you're looking for.[/quote]

understand the function, the problem is how to pass all parameters to the function you want. For the tutorial I call only for item validation. as in the example:

Code:
$ this-> form_validation->set_rules('username', 'Username', 'callback_username_check');
I can not use this scheme

Code:
$ This-> form_validation->set_rules('dateStart', 'Start Date', 'trim|required|callback_compareDataTime');
$ This-> form_validation->set_rules('dateEnd', 'Date endFim', 'trim |required|callback_compareDataTime');
$ This-> form_validation->set_rules('timeStart', 'Start Time', 'trim|required|callback_compareDataTime');
$ This-> form_validation->set_rules('timeEnd', 'End Time', 'trim|required|callback_compareDataTime');
so I'll just grab a parameter. I need to pass all the parameters of the function to test and return the message according to the function's return

#4
[eluser]Spectruman[/eluser]
anyone have any solution for it?

#5
[eluser]LuckyFella73[/eluser]
Hi spectruman,

I didn't test the script but you could do it this way:

1. set up a hidden form field (called "hidden_field" in my script)

and validate:
Code:
$this->form_validation->set_rules('dateStart', 'Start Date', 'trim|required');
$this->form_validation->set_rules('dateEnd', 'Date endFim', 'trim |required');
$this->form_validation->set_rules('timeStart', 'Start Time', 'trim|required');
$this->form_validation->set_rules('timeEnd', 'End Time', 'trim|required');
  
$this->form_validation->set_rules('hidden_field', 'Notice', 'callback_compareDataTime');
  

function compareDataTime()
{
    $dateStart = $this->input->post('dateStart'); // match your input-field-name here
    $timeStart = $this->input->post('timeStart');
    $dateEnd = $this->input->post('dateEnd');
    $timeEnd = $this->input->post('timeEnd');
  
    if ( ($timeStart == '12:00' && $timeEnd == '14:00') || ($timeStart == '14:00' && $timeEnd == '12:00') ||
              ($timeStart == '12:00' && $timeEnd <= $timeStart) || ($timeStart >= '12:00' && $timeEnd <= $timeStart) ||
              ($timeStart <= '12:00' && $timeEnd < $timeStart && $dateStart == $dateEnd) || ($timeStart == $timeEnd && $dateStart == $dateEnd) )
          {
        return false;
    }
    else
    {
        return true;
    }
}

This way you just check your callback function one time - what should be enough.

#6
[eluser]Kamarg[/eluser]
Perhaps try something like
Code:
$this-> form_validation->set_rules('dateStart', 'Start Date', 'trim|required|callback_compareDataTime[' . $this->input->post('post_var_to_compare_to') . ']');

You'll need to change the parameters that compareDataTime expects and you won't be able to store the rules in a config file this way but it should do the trick.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.