Unable to find callback validation rule codeigniter3

23 Views Asked by At

I am trying to validating recaptcha in codeigniter 3 callback validation. Debug output is -

Unable to find callback validation rule: validate_captcha

Seacrching on the internet ended up with this post. Can someone please help me figure this out.

In the Controller-

$setting_detail = $this->auth_model->settings_data();

    if ($setting_detail[0]['captcha'] == 0 && $setting_detail[0]['secret_key'] != null && $setting_detail[0]['site_key'] != null) {

        $this->form_validation->set_rules('g-recaptcha-response', 'recaptcha validation', 'required|callback_validate_captcha');
        $this->form_validation->set_message('validate_captcha', 'Please check the the captcha form');

        if ($this->form_validation->run() == FALSE) {
            $this->session->set_userdata(array('exception' => display('please_enter_valid_captcha')));
            $this->output->set_header("Location: " . base_url() . 'login', TRUE, 302);
        } }

The validate_captcha function-

  function validate_captcha() {
    $setting_detail = $this->auth_model->settings_data();
    $captcha        = $this->input->post('g-recaptcha-response');
    $response       = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=" . $setting_detail[0]['secret_key'] . ".&response=" . $captcha . "&remoteip=" . $_SERVER['REMOTE_ADDR']);
    if ($response . 'success' == false) {
        return FALSE;
    } else {
        return TRUE;
    }
}
1

There are 1 best solutions below

0
Sobuj Akand On

I have changed my code from

if ($this->form_validation->run() == FALSE)

To

if ($this->form_validation->run())

Now it works perfectly.