Welcome Guest, Not a member yet? Register   Sign In
Setting a variable equal to a form validation rule -- can this be done?
#1

[eluser]sehummel[/eluser]
Here is my CI form validation rule:
Code:
$datetime_string = $this->form_validation->set_rules('event_date', 'Select', 'callback_date_validate')
;

Here is my callback:

Code:
function date_validate($select_value)
{
    $year = '';
    $month = '';
    $day = '';
    $hour = '';
    $minutes = '';

    $datetime = $select_value;
    if (strpos($datetime, ' @ ') !== 'FALSE' && $datetime != '')
    {
        $datetime_explode = explode(' @ ', $datetime);
        if (strpos($datetime_explode[0], '/') !== 'FALSE' && $datetime_explode != '')
        {
            $date_explode = explode('/', $datetime_explode[0]);
            $year = $date_explode[2];
            $month = $date_explode[1];
            $day = $date_explode[0];    
        }
        if (strpos($datetime_explode[1], ':') !== 'FALSE')
        {
            $time_explode = explode(':', $datetime_explode[1]);
            $hour = $time_explode[0];
            if (strpos($time_explode[1], ' ') !== 'FALSE')
            {
                $minutes_explode = explode(' ', $time_explode[1]);
                $minutes = $minutes_explode[0];
                $am_pm = $minutes_explode[1];

                if ($am_pm == 'PM' || $am_pm == 'pm')
                    $hour += 12;    
            }
        }
    }

    $datetime_string = $year . '-' . $month . '-' . $day . ' ' . $hour . ':' . $minutes . ':00';

    if (!preg_match('/^\d{4}-\d{2}-\d{2} 2[0-3]|[01][0-9]:[0-5][0-9]:[0-5][0-9]$/', $datetime_string))
    {
        $this->form_validation->set_message('date_validate', 'Oops');
    }
    else // user picked something
    {
        return $datetime_string;
    }
}

According to the CI documentation, you can return data from a form validation callback, but by setting the rule equal to a variable, I get this error:

Quote:Object of class CI_Form_validation could not be converted to string

What am I doing wrong?
#2

[eluser]CroNiX[/eluser]
The only thing I'm seeing off the top of my head is you don't return FALSE if your rule fails. You only set a message.

Not having to do with your problem, but one thing you might consider is to prefix your validation function rule name with an underscore prefix so that it can't be accessed directly via the url. In your rules, you would also need to add an extra underscore like callback__function_name.
#3

[eluser]sehummel[/eluser]
I will do both.

I discovered something interesting. The callback rule seems to modify the post. When I comment out the rule, I get a different result than if I uncomment it. I do get the correct value back from the callback. That isn't explained very well in the documentation.
#4

[eluser]CroNiX[/eluser]
Yes, if you are manipulating and returning data instead of a boolean, it will enter that new data for the POST variable for that field so it can repopulate the form with the newly formatted data if the form fails.




Theme © iAndrew 2016 - Forum software by © MyBB