1

以下は私のコードのスニペットです:

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マニュアルに示されているように「コントローラー/メソッド」というラベルが付けられており、対応するコントローラー/メソッドが呼び出されたときに自動的に使用されます。

ログイン数が定義された数 (この場合は 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 に答える