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?


Messages In This Thread
Setting a variable equal to a form validation rule -- can this be done? - by El Forum - 02-28-2012, 10:30 AM



Theme © iAndrew 2016 - Forum software by © MyBB