Search code examples
phphtmlcodeigniter-3codeigniter-form-validation

Codeigniter(3) form validation always returns FALSE for GET method


codeigniter form validation is not working properly for GET method, it returns always false(even if all the input fields are correct) and not showing error messages. But when i tried with POST method, its working fine and showing error message if wrong input fields.

Below is HTML form with 3 input fields and all are mandatory required fields which is i'm checking in validation.

            **<form action="" method="get" name="ak-api-log" id="ak-api-log-form" autocomplete="off">**
                    <div class="row">
                        <div class="col-md-3">
                            <div class="form-group">
                              <label class="bmd-label-floating">API</label>
                              **<select name="ak_api_type" class="form-control">
                                <option value="all">All</option>
                                <option value="admin">Admin</option>
                                <option value="editor">Editor</option>
                              </select>**
                            <?php echo form_error('ak_api_type', '<div class="error">', '</div>'); ?>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                              <label class="bmd-label-floating">From</label>
                              **<input type="text" name="ak_api_from_date" class="form-control" id="ak-api-from-date"">**
                              <?php echo form_error('ak_api_from_date', '<div class="error">', '</div>'); ?>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                              <label class="bmd-label-floating">To</label>
                              **<input type="text" name="ak_api_to_date" class="form-control" id="ak-api-to-date">**
                              <?php echo form_error('ak_api_to_date', '<div class="error">', '</div>'); ?>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <button type="submit" id="userBtn" class="btn btn-primary pull-right"><i class="fa fa-search"></i> Search</button>
                        </div>
                    </div>
                **</form>**

Below is my controller code. I've removed codes not necessary for the question.

public function api_log(){
    $this->form_validation->set_rules('ak_api_type', 'ak_api_type', 'required');
    $this->form_validation->set_rules('ak_api_from_date', 'ak_api_from_date', 'required');
    $this->form_validation->set_rules('ak_api_to_date', 'ak_api_to_date', 'required');

    if ($this->form_validation->run() == FALSE){
        $this->template->view(strtolower(__CLASS__).'/'.__FUNCTION__);
    }else {
        echo 'true';exit;
    }
 }

Quick solutions will be really appreciated. Thanks in advance!


Solution

  • Codeigniter form-validation doesn't support GET method or any other method except POST.

    You may want to go for an alternative solution as below.

    
    // before creating the form validation rule.
    
    $dataToValidate = $this->input->get();
    
    // or you can set this data manually as below
    
    
    $dataToValidate = array(
            'username' => 'johndoe',
            'password' => 'mypassword',
            'passconf' => 'mypassword'
    );
    
    
    // followed by
    $this->form_validation->set_data($dataToValidate);
    
    

    You can set custom data easily in Codeigniter as above.

    Read reference: https://codeigniter.com/user_guide/libraries/form_validation.html#validating-an-array-other-than-post