下面是我的代碼片段:笨的reCAPTCHA和表單驗證配置
auth.php
// Login the user
public function login(){
$view_data = new stdClass;
// Form Data
$view_data->login_form = $this->auth_lib->get_login_form_data();
$view_data->reg_form = $this->auth_lib->get_reg_form_data();
$view_data->login_recaptcha = '';
$view_data->reg_recaptcha = '';
// Set an attempt
$this->auth_model->set_form_submit('Login');
// Get number of attemps
$login_count = $this->auth_model->get_form_submit_count('Login', 3600);
if($login_count >= 3){
// 3 or more attempts
$view_data->login_recaptcha = recaptcha();
$privkey = $this->config->item('recaptcha_private_key');
$remoteip = $_SERVER['REMOTE_ADDR'];
$challenge = $this->input->post('recaptcha_challenge_field');
$response = $this->input->post('recaptcha_response_field');
$resp = recaptcha_check_answer($privkey, $remoteip, $challenge, $response);
if($resp->is_valid !== TRUE){
$this->form_validation->set_rules('recaptcha_response_field', 'Recaptcha Response Field', 'required');
$this->form_validation->set_rules('recaptcha_challenge_field', 'Recaptcha Challenge Field', 'required|matches[recaptcha_response_field]');
}
}
if($this->form_validation->run() == FALSE){
// Not valid input
$template_data = $this->template->load('auth/login', $view_data);
$this->load->view($template_data->template, $template_data);
}else{
// Valid input
}
}
form_validation.php
$config = array(
'auth/login' => array(
array(
'field' => 'login_username',
'label' => 'Username',
'rules' => 'required|min_length[4]|max_length[12]|'
),
array(
'field' => 'login_password',
'label' => 'Password',
'rules' => 'required|min_length[6]|max_length[16]'
),
),
'auth/register' => array(
array(
'field' => 'reg_username',
'label' => 'Username',
'rules' => 'required|min_length[4]|max_length[12]|'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'required|min_length[3]|valid_email|'
),
array(
'field' => 'email_again',
'label' => 'Email Again',
'rules' => 'required|min_length[3]|valid_email|matches[email]'
),
array(
'field' => 'reg_password',
'label' => 'Password',
'rules' => 'required|min_length[6]|max_length[16]'
),
),
);
一切正常,我有我的表單驗證規則在配置文件「form_validation.php」中設置,並且如CI手冊中所示標記爲「controller/method」,以便在調用相應的控制器/方法時自動使用。
如果登錄計數大於或等於定義的金額(本例爲3),則顯示ReCaptcha表單。
問題是,如果用戶輸入錯誤的ReCaptcha並且未能輸入其他表單域中所需的信息,則只顯示ReCaptcha錯誤。如果用戶正確輸入ReCaptcha字段,並且未能通過輸入其他字段中的信息,則會顯示這些錯誤。
如果設置的驗證規則在控制器中,像這樣:
$this->form_validation->set_rules('login_username', 'Username', 'required');
然後將它是否存在的reCAPTCHA錯誤的旁邊,如果它存在就像你所期望的顯示錯誤。
我想要顯示任何或所有存在的錯誤,我怎麼能做到這一點不必在控制器中設置我的驗證?
我只是想念一些簡單的東西嗎?謝謝!