Welcome Guest, Not a member yet? Register   Sign In
Validation Message on upload file
#1

(This post was last modified: 12-19-2024, 07:51 AM by Anharr.)

Hi master, 
I have some functions to upload invoice images
This is my rules : 
PHP Code:
public function upload ()
{
   if (!$this->validate([
     'invoice' => [
     'rules'  =>    'uploaded[invoice]|is_image[invoice]|mime_in[invoice,image/jpg,image/jpeg,image/png]|max_size[invoice,2048]',
     'errors' => [
       'is_image' => 'Invoice must be pictures.',
       'mime_in'  => 'Invoice must be jpg/jpeg/png.',
       'max_size' => 'Invoice must be jpg/jpeg/png.',
       'uploaded' => 'Invoice must be uploaded.'
      ]
   ]
 ])) {
    $validation $this->validation;
    session()->setFlashdata('fail''something wrong, please check your input form!');
    return redirect()->back()->withInput()->with('validation'$validation);
  } else {
    echo "OK";
 
 }


This is my view : 
PHP Code:
<form action="/Management/upload" method="post" enctype="multipart/form-data">
<?= 
csrf_field() ?>
   <label for="invoice" class="form-label">Invoice</label>
   <input type="file" class="form-control <?= ($validation->hasError('invoice')) ? 'is-invalid' ''?>" id="invoice" name="invoice">
   <div class="invalid-feedback">
      <?= $validation->getError('invoice'); ?>
   </div>
</form> 

This is my controllers :
PHP Code:
public function invoice ()
{
  $data = [
    'code' => $entertaintCode,
    'company' => $this->CompanyModel->findAll(),
    'entertaint' => $checkEntertaint,
    'validation' => $this->validation
  
];
  return view('management/invoice'$data);


When I tried to upload files such as PDF or anything else except images, validation worked but there was no error message on my input file.
Reply
#2

What version of CodeIgniter are you running?
CodeIgniter 4 Users Guide - Library Reference - Validation
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 12-21-2024, 08:05 AM by Anharr.)

(12-19-2024, 10:27 PM)InsiteFX Wrote: What version of CodeIgniter are you running?
CodeIgniter 4 Users Guide - Library Reference - Validation

I use the 4.5.5 version. 

I think my validations are already followed as the documentation.
Reply
#4

Try dd($this->validation) after check. See all errors . Maybe you reset validation in redirect.
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#5

(12-23-2024, 11:40 AM)ozornick Wrote: Try dd($this->validation) after check. See all errors . Maybe you reset validation in redirect.

This is the result.

Dump and Die

I upload the pictures on my Google Drive.
Reply
#6

Hi, I’m also working on file upload validations in CodeIgniter 4. I wanted to ask if the issue might be related to how validation data is passed during a redirect? Could storing errors in session()->setFlashdata() and displaying them in the view help solve this?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB