2

我扩展了 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');

但它不起作用。我做错了什么?

4

2 回答 2

-1
$this->form_validation->set_rules('sponsor_id', 'Sponsor ID', 'trim|required|exact_length[7]|callback_check_first_char');

如果我没记错的话,你需要'callback_'

来源:http ://codeigniter.com/user_guide/libraries/form_validation.html

编辑:您还需要初始化 form_validation 库。

于 2012-04-20T22:12:26.690 回答
-1

确保在控制器中初始化库:

$this->load->library('form_validation');
于 2012-04-20T19:19:53.207 回答