Shiju
Shiju

Reputation: 1

form_validation->run() not properly working

 public function upload()
    {
        if (empty($_FILES['vchr_file']['name'])) {
            $this->form_validation->set_rules('vchr_file','File','required');
        }

        if ($this->form_validation->run()== true) 
        {
            echo 'valid';
        }
        else{

    $data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));


            $data['file']=array(
                        'name'  =>'vchr_file',
                        'type'  =>'file',
                        'label' =>'File Upload'
                        );

            $this->load->user_view('kyc',$data);
        }   
    }

my controller

div class="row">
                <div class="col-md-4">
                    <?php echo form_label($file['label']); 
                     echo form_input($file); ?>
                </div>
                <div class="col-md-2">
                    <input type="submit" name="upload" value="Upload">
                </div>
            </div>
        <?php echo form_close(); ?>

my view

the form_validation never run true.. and also when i upload a file the validation error not showing,but validation run() not working.

Upvotes: 0

Views: 78

Answers (1)

Alex
Alex

Reputation: 9265

When the file is actually uploaded the conditional set_rules statement doesn't apply the rule (which makes sense). However, form_validation always requires a rule to be set, if no rules are set (e.g. the conditional statement never goes through because a file is uploaded), than run() will always return false.

If you are planning on implementing more rules you shouldn't have a problem.

It is preferred to just handle it without form_validation and just a conditional statement with empty($_FILES['vchr_file']['name'] (again, if you aren't adding more rules).

Upvotes: 0

Related Questions