Reputation: 1202
I have checked all the questions of similar kind, and none of them are solving my problem, using CI 2.1.3, and HMVC from Wiredesignz.
I have in my form_validation.php config file the following rule:
array(
'field' => 'eta-renpal-1',
'label' => 'Renpal number (1)',
'rules' => 'required|callback_check_eta_group'
),
And in my ETA controller, I have this function (currently set to ALWAYS be invalid while testing):
public function check_eta_group($reference)
{
// Internal function for use by form validation system to check if the ETA group requirements are met.
$this->form_validation->set_message('check_eta_group', 'Other values in the group ' . $reference . ' are also required.');
return false;
}
For some reason, the "required" function works, but the callback does not. I have tried all other similar suggested solutions, and can't get them to work. Please help?
Edit: The callback does not appear to be called at all. I even did var_dump() in the callback to see if there is output on the screen - none...
Edit2:: See last comment by myself - using that work-around solves the problem, but it is not exactly what I wanted. So - if you have a better solution, please share :-)
Upvotes: 4
Views: 5209
Reputation: 31
make sure your function checks are inside the same controller that you are actually running the custom checks in (i.e. it should be able to be called with self::check_eta_group)
I had trouble getting validation working with my checks inside of MY_Controller for instance. But when i moved them into the extended controller it worked fine.
here are two checks and how I called them (all within the same controller)
// custom form validators for datepicker and timepicker
public function date_valid($date){
$month = (int) substr($date, 0, 2);
$day = (int) substr($date, 3, 2);
$year = (int) substr($date, 6, 4);
$this->form_validation->set_message('date_valid', 'The %s field is not a valid date');
return checkdate($month, $day, $year);
}
public function time_valid($time){
$this->form_validation->set_message('time_valid', 'The %s field is not a valid time');
if (preg_match("/^(1[0-2]|0?[1-9]):[0-5][0-9] (AM|PM)$/i", $time)) {
return TRUE;
} else {
return FALSE;
}
}
public function create_custom(){
// load models and libraries
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
// set form validation rules
$this->form_validation->set_rules('schedule_date', 'Schedule Date', 'required|callback_date_valid');
$this->form_validation->set_rules('schedule_time', 'Schedule Time', 'required|callback_time_valid');
....
if ($this->form_validation->run() == FALSE) { // failed validation
error_log("validation_errors: ".validation_errors());
}
Upvotes: 0
Reputation: 1202
See my last comment under the question
(Using a workaround explained here, stackoverflow.com/questions/3029717/…, it works. It is not the way I want it to work with callbacks, but as long as it works, it is probably alright. Thanks anyways.)
Thanks Frosty for your comments.
Upvotes: 2