无法访问与您的字段名称对应的错误消息


Unable to access an error message corresponding to your field name

我有一个回调函数,check_captcha它查看$row==0还是== 1(此信息是从sql查询的(。

问题是我无法从$self->form_validation->set_rule('captcha', 'call_back_check_captcha')调用它,因为我的函数接受一个$row变量。我现在调用它的方式收到"无法访问"错误消息。我怎样才能做到这一点?

function check_captcha( $row)
{
    if($row ==0)//didnt find any
    {
        $this->form_validation->set_message('captcha', 'text dont match captcha');
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}

function create_member() 
        {   
            $past = time() - 7200; 
        $this->db->query("DELETE FROM captcha WHERE captcha_time <".$past);                 
        $sql = "SELECT COUNT(*) AS count FROM captcha WHERE word =? AND ip_address =?";     
        $binds = array($_POST['captcha'], $this->input->ip_address(), $past);
        $query= $this->db->query($sql, $binds);
        $row = $query->row(); //row query rows : if it found an entry =1 
            $self->check_captcha($row->count);
        //VALIDATIONS
        $this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
        $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');      
        $this->form_validation->set_rules( 'email_address', 'Email Address', 'trim|required|valid_email|unique[user.email_address]');
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|unique[user.username]');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_leng[32]');
        $this->form_validation->set_rules('password2', 'Password Confirmation','trim|required|matches[password]');
        if(!$_POST['captcha']){
        $this->form_validation->set_rules('captcha', 'Captcha','trim|required');}else{
        $this->form_validation->set_rules('captcha', 'Captcha', 'callback_check_captcha');}
        if($this->form_validation->run()==FALSE)
        {   //this -> to the curr obj(UserController) && registraion() points to the the function in controller
            $this->registration();  //reloads reg page so they can fill out right stuff
        }
        else
$this->form_validation->set_message('check_captcha', 'text dont match captcha');

消息名称对应于函数,而不是字段。因此,将其设置为">check_captcha"将修复您的错误。错误消息将使用正确的字段名称。

实际上,最好的方法不是直接在控制器上编写错误消息,而是在语言上添加此条目"check_captcha"。

就我而言,验证规则(表单验证("less_than"的消息不存在。

我更改了文件/系统/语言/??/form_validation_lang.php.我添加了缺少的条目。

这对

我有帮助

转到应用程序/配置/自动加载.php并在那里添加"安全"帮助程序类。

$autoload['helper'] = array('security');

或者在表单验证之前添加此内容

$this->load->helper('security');

您可以在set_rules中设置错误消息

$this->form_validation->set_rules('captcha', 'Captcha', 'callback_check_captcha',

array('check_captcha' => 'text dont match captcha'));

在语言文件中添加一个条目,并在错误消息的 (yourfieldname( 内使用部分命名 - 这解决了问题

即使问题已经得到回答,还有另一个错误可能导致相同的错误消息:

如果您调用回调检查验证码,它将不起作用。始终首选像 Codeigniter/General Topics/PHP 风格指南中推荐的 check_captcha 这样的名称。