我正在尝试在CodeIgniter中实现recaptcha我的表单(不使用recaptcha库)。这很好用,但是在我的表单中,我单独显示每个字段的错误,我想在表单中的地方旁边显示重新接收错误,有人可以帮助我,我该怎么办?
我的控制器的代码:
$this->form_validation->set_rules('name', 'Name', 'trim|required|min_length[3]|max_length[25]');
$this->form_validation->set_rules('email', 'Email address', 'trim|required|valid_email');
//With above set_rules i'm able to display each fields errors next to it,
//How can i display following recaptcha error next to it.
if (!$resp->is_valid)
{
//reCAPTCHA was entered incorrectly
die (
"The reCAPTCHA wasn entered incorrectly." .
"(reCAPTCHA said: " . $resp->error . ")
");
}
else
{
//Successful verification
die('Success!');
}
感谢您的帮助。
答案 0 :(得分:0)
如果您没有通过表单验证库运行recaptcha字段,则无法使用form_error()
函数,但您可以轻松地将错误作为变量发送到视图中:
if ($this->form_validation->run())
{
if ( ! $resp->is_valid)
{
$data['recaptcha_error'] = "reCAPTCHA said: ".$resp->error;
}
else
{
// Process form
}
}
$this->load->view('my_view', $data);
然后在你看来(无论你想要的地方):
<?php if (isset($recaptcha_error)): ?>
<label class="error">
<?php echo $recaptcha_error; ?>
</label>
<?php endif; ?>