2013-06-04 69 views
1

下面是我的代碼片段:笨的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錯誤的旁邊,如果它存在就像你所期望的顯示錯誤。

我想要顯示任何或所有存在的錯誤,我怎麼能做到這一點不必在控制器中設置我的驗證?

我只是想念一些簡單的東西嗎?謝謝!

回答

0

現在,直到我找到一個更好的解決方案,允許我使用form_validation.php配置文件,我決定使用下面的這個方法。

public function register(){ 

     if($login_count >= 3){ 
     // 3 or more attempts 

     $view_data->reg_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(isset($resp->is_valid) && ($resp->is_valid !== TRUE)){ 

     $this->set_validation_rules('register', true);  
    }else{ 
     $this->set_validation_rules('register', false); 
    }  

    if($this->form_validation->run() == FALSE){ 
     // Not valid input 
     $template_data = $this->template->load('auth/register', $view_data); 
     $this->load->view($template_data->template, $template_data);  

    }else{ 
     // Valid input 

    } 
} 


public function set_validation_rules($form = '', $recaptcha = FALSE){ 

    if($recaptcha !== FALSE){ 
     $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($form === 'login'){ 
     $this->form_validation->set_rules('login_username', 'Username', 'required'); 
     $this->form_validation->set_rules('login_password', 'Password', 'required'); 
    } 

    if($form === 'register'){ 
     $this->form_validation->set_rules('reg_username', 'Username', 'required|min_length[4]|max_length[12]'); 
     $this->form_validation->set_rules('email', 'Email', 'required|min_length[3]|valid_email'); 
     $this->form_validation->set_rules('email_again', 'Email Again', 'required|min_length[3]|valid_email|matches[email]'); 
     $this->form_validation->set_rules('reg_password', 'Password', 'required|min_length[6]|max_length[16]');   
    } 

這不是我想要做的,如上所述,我想保留我的驗證配置文件。所以如果你有更好的解決方案,請分享!在此之前,這將做。

謝謝!