我已经检查了所有类似的问题,但没有一个能解决我的问题,使用CI 2.1.3和Wiredesignz的HMVC。
我的form_validation.php配置文件中有以下规则:
array(
'field' => 'eta-renpal-1',
'label' => 'Renpal number (1)',
'rules' => 'required|callback_check_eta_group'
),
在我的ETA控制器中,我有这个函数(当前设置为在测试时始终无效):
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;
}
出于某种原因,"必需"函数有效,但回调不起作用。我已经尝试了所有其他类似的建议解决方案,但无法让它们工作。请帮忙?
编辑:回调似乎根本没有被调用。我什至在回调中做了 var_dump() 以查看屏幕上是否有输出 - 没有...
Edit2::请参阅我自己的最后一条评论 - 使用该解决方法可以解决问题,但这并不完全是我想要的。所以 - 如果您有更好的解决方案,请分享:-)
请参阅我在问题下的最后一条评论
(使用此处解释的解决方法,stackoverflow.com/questions/3029717/...,它有效。这不是我希望它与回调一起工作的方式,但只要它有效,它可能没问题。无论如何,谢谢。
感谢Frosty的评论。
确保您的函数检查位于您实际运行自定义检查的同一控制器中(即它应该能够使用 self::check_eta_group 调用)
例如,我在MY_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());
}