Reputation: 5568
Here is my CI form validation rule:
$datetime_string = $this->form_validation->set_rules('event_date', 'Select', 'callback_date_validate');
Here is my callback:
function date_validate($select_value)
{
$year = '';
$month = '';
$day = '';
$hour = '';
$minutes = '';
$datetime = $this->input->post('event_date');
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:
Object of class CI_Form_validation could not be converted to string
What am I doing wrong?
Upvotes: 2
Views: 2389
Reputation: 2013
I don't know if you ever solved this but I was scratching around the documentation wondering the same thing as your question.
First of all, you just want...
$this->form_validation->set_rules('event_date', 'Select', 'callback_date_validate');
Don't assign it to a variable.
Next, you want to run your validation rules/callbacks...
if ($this->form_validation->run() == TRUE) {
// VALIDATION OK, CHECK OUR CALLBACK DATA
var_dump($this->input->post());
}
Now you should see your data returned from the callback in... $this->input->post('event_date');
The thing that confused me about returning data from callbacks was this thread on the official CI forums... http://codeigniter.com/forums/viewthread/191087/ where people are suggesting that the callback doesn't change the POSTed data. They are only partially correct. It doesn't change it under $_POST['whatever']
but it does change it in $this->input->post('whatever')
As this was the 3rd result in Google I hope this helps someone out.
Upvotes: 0
Reputation: 4250
I think the problem is in this line:
$datetime = $this->input->post('event_date');
The value of event_date field is captured in a parameter of your function $select_value try using your parameter instead of post data.
Upvotes: 1
Reputation: 7388
$this->form_validation->set_rules(...);
should be called inline and not assigned to a variable. You have assigned it to $datetime_string
. Remove this assignment.
Upvotes: 0