1

下面是我的代码片段:

授权文件

    // 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 手册中所示的标记为“控制器/方法”。

如果登录计数大于或等于定义的数量(在本例中为 3),则显示 ReCaptcha 表单。

问题是这样的,如果用户输入了错误的 ReCaptcha 并且未能在其他表单字段中输入所需的信息,那么只会显示 ReCaptcha 错误。如果用户正确输入了 ReCaptcha 字段并且未能在其他字段中输入信息,则会显示这些错误。

如果像这样在控制器中设置验证规则:

$this->form_validation->set_rules('login_username', 'Username', 'required');    

然后,如果它存在于 ReCaptcha 错误旁边,如果它像您期望的那样存在,则会显示该错误。

我想显示任何或所有存在的错误,我怎样才能做到这一点而不必在控制器中设置我的验证?

我只是缺少一些简单的东西吗?谢谢!

4

1 回答 1

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]');         
    }

这不是我想做的,如上所述,我想将我的验证保存在配置文件中。因此,如果您有更好的解决方案,请分享!在那之前,这将是可行的。

谢谢!

于 2013-06-04T02:58:00.947 回答