Codeigniter验证错误:"无法访问与您的字段名称Captcha对应的错误消息。 (验证码)"

时间:2017-01-24 07:04:44

标签: php codeigniter curl model callback

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

但我一直遇到这个问题: 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似乎根本没有运行回调函数。

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

The Username field is required. The Password field is required. The Password Confirmation field is required. The Email field is required.

我通过我的模型运行验证(遵循保持控制器苗条和模型脂肪的惯例):

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回调函数:

/*
 * 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个小时...我希望代码是可读的。

感谢〜

1 个答案:

答案 0 :(得分:1)

  

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

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

尝试设置如下规则

$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')
              )
);

$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'
             )
);

查看更多here