Welcome Guest, Not a member yet? Register   Sign In
Validate file inside controller in Codeigniter4
#1

(This post was last modified: 03-19-2024, 10:15 PM by sarath_unrelax.)

Hello,

I'm trying to create a simple HTML form with one input field which accept one file (csv,xlsx) and i will process it and insert into my database. I need to validate the file with two things
1. the file type should be .csv or .xlsx
2. required filed
3. size should be less than 1 mb

My Controller Function :

Code:
Public function bulkUpload() {
        $rules = [

            'file' => 'required|mime_in[file,text/csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet]|max_size[file,1024]'
        ];

        $data = $this->request->getPost(array_keys($rules));
        if (! $this->validateData($data, $rules)) {

            echo "failed";
        }
        else {

            echo "success";

        }
   
        return view('employees/bulk-upload/index');


    }


My Form :

Code:
<?php echo form_open_multipart('dashboard/employees/bulk-upload'); ?>


                <div class="card mb-4 mt-5">
                    <h5 class="card-header"></h5>

                    <!-- Account -->
                    <div class="card-body">

                      <div class="mb-5 col-md-6">
                        <label for="formFile" class="form-label">Select File </label>

                        <input class="form-control" name="file" type="file" id="formFile">                     
   
                      <div class="mb-3">

                                <p class="text-muted mb-0">Allowed .csv, .xls  or xlsx. Max size of 800K</p>
                             

                        </div>

                        <div class="mb-3">

                            <p class="text-muted mb-0"><a href="#">Download Template</a></p>

                        </div>

                        <div class="mt-2">
                                <button type="submit" class="btn btn-primary me-2">Save changes</button>
                                <button type="reset" class="btn btn-outline-secondary">Cancel</button>
                            </div>                       
                   

                </div>
</form>


But the problem, i'm unable to validate here, this always give me error.
Anyone can help me how i can validate this file inside the controler and show the error message in my view ?
Reply
Reply
#3

(This post was last modified: 03-19-2024, 10:58 PM by sarath_unrelax.)

(03-19-2024, 10:30 PM)kenjis Wrote: See https://codeigniter4.github.io/CodeIgnit...he-process
https://codeigniter4.github.io/CodeIgnit...le-uploads

Thanks, I got it. But how i can set custom validation error message for this rule

Code:
  $rules = [

               $rules = [

            'file' => 'uploaded[file]|max_size[file,1024]|mime_in[file,text/csv,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet]',
        

        ];

            if($this->request->getFile("file")) {

            $data = $this->request->getPost(array_keys($rules));

            if (! $this->validateData($data, $rules)) {
                
                $errors = $this->validator->getErrors();

    
                return redirect()
                            ->back()
                            ->with('warning', 'Please fix following error')
                            ->with('errors', $errors);    
            }
Reply
#4

CodeIgniter 4 User Guide - Validation - Setting Custom Error Messages
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB