我已经扩展了CI表单验证库,如下所示:
class MY_Form_validation extends CI_Form_validation {
function __construct($config = array())
{
parent::__construct($config);
}
function check_first_char($str)
{
$CI =& get_instance();
$first_char = substr($str, 0, 1);
if ($first_char != 'P' || $first_char != 'S')
{
$CI->form_validation->set_message('check_first_char', 'The %s field must begin with P or S!');
return FALSE;
}
else {
return TRUE;
}
}
并将其称为以下内容:
$this->form_validation->set_rules('sponsor_id', 'Sponsor ID', 'trim|required|exact_length[7]|check_first_char');
但它不起作用。我做错了什么?
答案 0 :(得分:-1)
确保在控制器中初始化库:
$this->load->library('form_validation');
答案 1 :(得分:-1)
$this->form_validation->set_rules('sponsor_id', 'Sponsor ID', 'trim|required|exact_length[7]|callback_check_first_char');
如果我没弄错的话,你需要'回调_'
来源:http://codeigniter.com/user_guide/libraries/form_validation.html
编辑:您还需要初始化form_validation库。