Welcome Guest, Not a member yet? Register   Sign In
validation and callback functions
#1

[eluser]jbads[/eluser]
Hi what is the best way to do the following:

I have a form where users must enter 2 different dates. One for the actual date of the event they're promoting and one for the date their promotional material is to go live on my website.

For each date I would like 3 dropdowns one each for day, month and year. At the validation stage I would like to check whether the date has not already passed and that the event date comes after the promotional date.

I tried this in my controller
Code:
$e_date = $this->input->post('event_date_day').'/'.$this->input->post('event_date_month').'/'.$this->input->post('event_date_year');
        $a_date        =    $this->input->post('active_date_day').'/'.$this->input->post('active_date_month').'/'.$this->input->post('active_date_year');

        
    //Validate form data
        $rules['event_date']        =        'trim|xss_clean|min_length[2]|callback__valid_date[$e_date]';
        $rules['active_date']        =        'trim|xss_clean||min_length[2]|callback__valid_date[$a_date]';

And this in the callback function
Code:
function _valid_date($x_date){

//Prepare timestamp for entered date
        $date                =            explode('/', $x_date);

        $a_timestamp            =            mktime(00,00,01,$a_date[1],$a_date[0],$a_date[2]);
        
    //Set time zone to use and create timestamp for current time
        date_default_timezone_set('Pacific/Auckland');
        $now                     =             time();
                
        
    //Check that the date has not already passed, that the months are not greater than 12 and the days not greater than 31
        if($e_timestamp < $now || $date[1] > 12 || $date[0] > 31)
        {
            $this->validation->set_message('_valid_date', 'Please make you enter a valid date.');
            return FALSE;        
        }
        else
        {
            return TRUE;
        }
}

It seems callback functions will only send the value of the current input field and wont pass the callback function the extra variable containing the date...

So It's only passing the callback the year field instead of all three fields such as 01/10/09

Is there a way to pass a callback function those variables. I have also tried setting those variables inside the callback function but then how do I check both dates individually and return the message to the correct date entry section of the form?
#2

[eluser]Colin Williams[/eluser]
$this->input->post('other_field')
#3

[eluser]jbads[/eluser]
Please eleborate?
#4

[eluser]Colin Williams[/eluser]
In your custom callback function, use $this->input->post() to capture the other field because you cannot pass two vars into a callback.
#5

[eluser]Chee Wai[/eluser]
It didn't work because in your case:

$rules['event_date'] = 'trim|xss_clean|min_length[2]|callback__valid_date[$e_date]';

you enclosed your php variable in single quotes.
the date field is passed as the literal $e_date.

To pass the value, you should use double quotes:
$rules['event_date'] = "trim|xss_clean|min_length[2]|callback__valid_date[$e_date]";

or string concatenation:
$rules['event_date'] = 'trim|xss_clean|min_length[2]|callback__valid_date[' . $e_date . ']';
#6

[eluser]Colin Williams[/eluser]
I didn't think you could even pass extra values to the callback functions. I thought the callback only ever was supplied one argument, the value of the field being validated.




Theme © iAndrew 2016 - Forum software by © MyBB