Php CodeIgniter-验证码回调验证不工作

Php CodeIgniter-验证码回调验证不工作,php,codeigniter,validation,callback,captcha,Php,Codeigniter,Validation,Callback,Captcha,我想要一些帮助 这是保存联系人表单上所有验证的数组 class Contact_Form extends CI_Controller { private $_validation = array( 'fullname' => array( 'field' => 'fullname', 'label' => 'Fullname', 'rules' => 'trim|require

我想要一些帮助

这是保存联系人表单上所有验证的数组

class Contact_Form extends CI_Controller
{
    private $_validation = array(
        'fullname' => array(
            'field' => 'fullname',
            'label' => 'Fullname',
            'rules' => 'trim|required|max_length[255]'
        ),
        'email' => array(
            'field' => 'email',
            'label' => 'Email Address',
            'rules' => 'trim|required|max_length[255]|valid_email'
        ),
        'phone' => array(
            'field' => 'phone',
            'label' => 'Phone',
            'rules' => 'trim|max_length[10]|integer'
        ),
        'message' => array(
            'field' => 'message',
            'label' => 'Message',
            'rules' => 'trim|required'
        ),
        'captcha' => array(
            'field' => 'captcha',
            'label' => 'Security Code',
            'rules' => 'trim|required|callback_validate_captcha'
        )
    );

// This is the part where I validate my contact form inside a method
$this->load->library('form_validation');
        $this->form_validation->set_rules($this->_validation);
        if ($this->form_validation->run() === true) {
            echo 'works!';  
        }
这是验证验证码的回调函数

public function callback_validate_captcha($str) {
        $post_captcha = $this->input->post('captcha');
        $set_captcha = $this->session->userdata('captcha');

        if (strcmp($set_captcha, $post_captcha) !== 0) {
            $this->form_validation->set_message('validate_captcha', '%s is wrong');
            return false;
        }       
        return true;
    }
如果我在一个空表单上点击submit,我会得到一个错误,表明captcha是必填字段,但是如果我提交了一个错误的代码,我不会得到任何错误,这意味着回调被忽略

我试图改变我的if语句

// change this (althought i feel its more correct)
if (strcmp($set_captcha, $post_captcha) !== 0)

// to this
if ($set_captcha != $post_captcha)

但问题依然存在。你知道怎么回事吗?

你犯了一个大错误,你必须使用函数验证码而不是回调验证码。
因为callback是调用函数的form关键字,所以请尝试一下,宾果(bingo)

这里有一个问题我可以初步看到,将
callback\u validate\u captcha
更改为
validate\u captcha
。当您为回调方法命名方法时,它们不需要回调前缀。您的意思是在这里$this->form\u validation->set\u message('validate\u captcha','%s is error')??不知道控制器中的实际方法名称,其他一切看起来都正常。将
public function callback\u validate\u captcha($str)
更改为
public function validate\u captcha($str)
你说得对!我在处理和粘贴名称时忘记删除“回调”部分。愚蠢的错误:)是的,我发现了它,就像麦克说的那样,并修复了它。