问题
I'm trying to add Google's reCAPTCHA into my Codeigniter web app.
But I keep running into this problem:
Unable to access an error message corresponding to your field name Captcha. (recaptcha)
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):
The Username field is required.
The Password field is required.
The Password Confirmation field is required.
The Email field is required.
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;
}
}
}
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;
}
}
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.
Thanks~
回答1:
“Unable to access an error message corresponding to your field name"
I guess Above error is shown when a field has no value and no required rule:
Try setting rule like below
$this->form_validation->set_rules('g-recaptcha-response', 'Captcha', 'required|callback_recaptcha');
OR ( For recent version of 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'
)
);
See more here
来源:https://stackoverflow.com/questions/41821994/codeigniter-validation-error-unable-to-access-an-error-message-corresponding-t