我在代码点火器验证中有一个自定义回调没有被调用,我只是不知道错误在哪里。
这是我的控制器:
$this->form_validation->set_error_delimiters('', '');
$this->form_validation->set_rules('pedido', 'numero de pedido', 'trim|required|integer');
$this->form_validation->set_rules('cliente', 'nombre del cliente', 'trim|required|alpha_numeric_spaces');
$this->form_validation->set_rules('motivo', 'motivo de la devolucion', 'trim|required|alpha_numeric_spaces');
$this->form_validation->set_rules('imagen', 'imagen', 'callback_handle_upload');
if ($this->form_validation->run() == FALSE) {
$error_array = array(
'pedido' => form_error('pedido'),
'cliente' => form_error('cliente'),
'motivo' => form_error('motivo'),
'imagen' => form_error('imagen')
);
$this->session->set_userdata('notify', $error_array);
redirect('garantias-y-devoluciones');
die();
}
这是我的回调函数
public function handle_upload($str)
{
$this->form_validation->set_message('imagen', 'The {field} has an error.');
return FALSE;
}
它应该触发错误,但不是。
已解决。!如果有人遇到同样的错误,我就是这样解决的,这是HMVC的一个错误,"在将当前控制器作为$CI变量分配给form_validation库之前。这将允许您的回调方法正常工作。"
只需在MY_Form_validation 中添加public $CI
然后在你的控制器构造函数上:
$this->load->library('form_validation');
$this->form_validation->CI =& $this;
有关更多信息,请访问以下页面:https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc
将回调方法重命名为"handle_upload"。例如:
/* ... your code ... */
$this->form_validation->set_rules('imagen', 'imagen', 'callback_handle_upload');
/* ... your code ... */
public function handle_upload($msg){/* your validation code */}
我知道情况并非总是如此,但如果你使用的是Ubuntu,最好检查一下你是否启用了mod重写,如果没有,Apache会加载自己的文件,而不是codeigniter->database.php文件。。。
只需将回调函数重命名为handle_upload
即可。正如CI文档中所说的To invoke a callback just put the function name in a rule, with "callback_" as the rule prefix.