Codeigniter验证错误:“无法访问与您的字段名称验证码相对应的错误消息.(验证码)" [英] Codeigniter Validation error: "Unable to access an error message corresponding to your field name Captcha. (recaptcha)"

查看:53
本文介绍了Codeigniter验证错误:“无法访问与您的字段名称验证码相对应的错误消息.(验证码)"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Google的reCAPTCHA添加到我的Codeigniter网络应用中.

I'm trying to add Google's reCAPTCHA into my Codeigniter web app.

但是我一直遇到这个问题:无法访问与您的字段名Captcha相对应的错误消息.(验证码)

But I keep running into this problem: Unable to access an error message corresponding to your field name Captcha. (recaptcha)

我尝试提交表单以引发错误, $ server_output ['success'] 应该为 FALSE ,从而执行 $ this-> form_validation-> set_message('recaptcha','Please redo the {field}.'); ,但Codeigniter似乎根本没有运行回调函数...

I try submitting the form to throw an error, $server_output['success'] should be FALSE thus executing $this->form_validation->set_message('recaptcha', 'Please redo the {field}.'); but Codeigniter doesn't seem to be running the callback function at all...

验证会作为错误输出(除了标题中的错误消息外)对其他所有内容起作用:

The Validation is working for everything else as the error outputs (in addition to the error message in the title):

用户名"字段是必填字段.密码字段是必填字段.密码确认字段是必填字段.电子邮件字段为必填.

我正在通过我的模型进行验证(遵循保持控制器苗条和模型胖的惯例):

I'm running the verification through my model (following the convention of keeping controllers slim and models fat):

public function __construct()
{
    parent::__construct();
    $this->load->library('form_validation');
}

public function register_validation()
{
    /*
     * INITIALIZE FORM VALIDATION RULES
     */
    $this->form_validation->set_rules('username', 'Username',
        array(
            // REMOVED FOR PRIVACY REASONS.
        )
    );
    $this->form_validation->set_rules('password', 'Password',
        array(
            // REMOVED FOR PRIVACY REASONS.
            )
    );
    $this->form_validation->set_rules('password_confirmation', 'Password Confirmation',
        array(
            // REMOVED FOR PRIVACY REASONS.
            )
    );
    $this->form_validation->set_rules('email', 'Email',
        array(
            // REMOVED FOR PRIVACY REASONS.
        )
    );

    /*
     * SET CUSTOM VERIFICATION RULES:
     * 1. GOOGLE RECAPTCHA WIDGET
     */
    $this->form_validation->set_rules('g-recaptcha-response', 'Captcha', 'callback_recaptcha');

    /*
     * CHANGE ERROR MESSAGES FOR SOME RULES.
     */
    $this->form_validation->set_message('is_unique', 'The {field} has already been taken!');

    /*
     * RUN VALIDATION,
     * IF VALIDATION IS NOT PASSED --> RETURN FALSE.
     */
    if ($this->form_validation->run() === FALSE)
        return FALSE;
    /*
     * ELSE WHEN EVERYTHING PASSES VALIDATION AND RECAPTCHA,
     * TRY TO CREATE NEW USER.
     */
    else
    {
        /*
         * TRY TO CREATE NEW USER.
         */
        if ($this->create_user())
        {
            /*
             * SET FLASHDATA TO ALERT USER THAT
             * THE ACCOUNT HAS BEEN SUCCESSFULLY CREATED.
             */
            $this->session->account_created = TRUE;
            $this->session->mark_as_flash('account_created');
            return TRUE;
        }
        else
        {
            /*
             * THROW ERROR AND ALERT USER VIA FLASHDATA.
             */
            $this->session->account_created = FALSE;
            $this->session->mark_as_flash('account_created');
            return FALSE;
        }
    }
}

这是我在同一类/模型中的recaptcha回调函数:

Here is my recaptcha callback function in the same class/model:

/*
 * RECAPTCHA FUNCTION
 */
public function recaptcha($data = '')
{
    /*
     * INITIALIZE VARIABLES.
     */
    $google_api_url = 'https://www.google.com/recaptcha/api/siteverify';
    $google_secret_key = 'REMOVED FOR PRIVACY REASONS';
    $ip = $this->input->ip_address();
    $response = $data;

    /*
     * INITIALIZE CURL.
     */
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $google_api_url);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS,
        http_build_query(array(
                'secret' => $google_secret_key,
                'response' => $response,
                'remoteip' => $ip,
            )
        )
    );
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

    /*
     * RUN CURL.
     */
    $server_output = curl_exec($curl);
    curl_close($curl);
    $server_output = json_decode($server_output, true);

    /*
     * CHECK RECAPTCHA SUCCESS
     */
    if ($server_output['success'])
        return TRUE;
    else
    {
        $this->form_validation->set_message('recaptcha', 'Please redo the {field}.');
        return FALSE;
    }
}

我做错了什么?因为我正在模型中运行验证,所以无法正常工作吗?在这个问题上停留了2-3个小时...我希望代码可读.

What am I doing wrong? Is it not working because I'm running the validation within a model? Been stuck on this for a good 2-3 hours... I hope the code is readable.

谢谢〜

推荐答案

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

我猜想当一个字段没有值且没有必需的规则时会显示上述错误:

I guess Above error is shown when a field has no value and no required rule:

尝试如下设置规则

$this->form_validation->set_rules('g-recaptcha-response', 'Captcha', 'required|callback_recaptcha');

或(对于CI的最新版本)

$this->form_validation->set_rules(
        'g-recaptcha-response', /* Field */
        'Captcha',              /* Label */
         array(                 /* Rules */
                'required',
                array($this->your_model, 'recaptcha')
              )
);

OR

$this->form_validation->set_rules(
        'g-recaptcha-response', /* Field */
        'Captcha',              /* Label */
         array(                 /* Rules */
                'required',
                array('my_recaptcha', array($this->your_model, 'recaptcha'))
              ),
        array(                 /* Error lists */
              'my_recaptcha' => 'Invalid Recaptcha'
             )
);

查看更多此处

这篇关于Codeigniter验证错误:“无法访问与您的字段名称验证码相对应的错误消息.(验证码)"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆