Specifying Custom Attribute Values does not work Laravel

78 views Asked by At

I have the following code:

use Illuminate\Support\Facades\Validator;

$rules = array(
     'attachment' => 'max:1000|file|mimes:jpeg,png,jpg,gif,svg,pdf',
);

$messages = array(
     'attachment.max' => 'max error',
     'attachment.file' => 'file error',
     'attachment.mimes' => 'mimes error',
     //'attachment' => 'all errors',
);

$validator = Validator::make($this->request->all(), $rules, $messages);

I'm trying to apply the concept of "Specifying Custom Attribute Values": https://laravel.com/docs/9.x/validation#specifying-custom-attribute-values

I'm using Laravel: "laravel/framework": "^9.19"

However, it doesn't work, the messages are not identified according to the specific error, and the following is displayed:

dd($validator->messages());

Illuminate\Support\MessageBag {#1547
   #messages: array:1 [
     "attachment" => array:1 [
       0 => "The attachment failed to upload."
     ]
   ]
   #format: ":message"
}

Where is the error? Can anyone help me and explain to me the reason for this behavior?

I want to use a specific message for each type of error, I don't want to uncomment //'attachment' and not have control of where the upload is failing.

1

There are 1 answers

2
José Victor On

I managed to find the reason for my problem above.

In fact, the problem is not in this code, it is in the file that I am sending to the server and, for some reason, Laravel/PHP cannot retrieve important information to validate correctly, as follows:

In the correct scenario, where it worked as I expected, the server printout is: (notice that Laravel managed to get the size and other values)

Illuminate\Http\UploadedFile {#1523 // app/Validators/OrderFileValidator.php:58
   -test: false
   -originalName: "2023-11-12 09-17-00.mp4"
   -mimeType: "video/mp4"
   -error: 0
   #hashName: null
   path: "/tmp"
   filename: "phpsItJmi"
   basename: "phpsItJmi"
   pathname: "/tmp/phpsItJmi"
   extension: ""
   realPath: "/tmp/phpsItJmi"
   aTime: 2024-02-12 10:33:26
   mTime: 2024-02-12 10:33:26
   cTime: 2024-02-12 10:33:26
   inode: 3805560
   size: 1906989
   perms: 0100600
   owner: 33
   group: 33
   type: "file"
   writable: true
   readable: true
   executable: false
   file: true
   dir: false
   link: false
}

Illuminate\Support\MessageBag {#1547
   #messages: array:1 [
     "attachment" => array:2 [
       0 => "The maximum file size is 1 mb (megabyte)"
       1 => "The extensions allowed for the file are: jpeg,png,jpg,gif,svg,pdf"
     ]
   ]
   #format: ":message"
}

Now, for the invalid file (the reason for opening this question), Laravel/PHP read the following values: (Note that it can't get the file size and several other attributes)

Illuminate\Http\UploadedFile {#1523 // app/Validators/OrderFileValidator.php:58
   -test: false
   -originalName: "Presentation - José Victor_20240125_070758_0000.pdf"
   -mimeType: "application/octet-stream"
   -error: 1
   #hashName: null
   path: ""
   filename: ""
   basename: ""
   pathname: ""
   extension: ""
   realPath: "/var/www/html/app/public"
   aTime: 1969-12-31 21:00:00
   mTime: 1969-12-31 21:00:00
   cTime: 1969-12-31 21:00:00
   inode: false
   size: false
   perms: 00
   owner: false
   group: false
   type: false
   writable: false
   readable: false
   executable: false
   file: false
   dir: false
   link: false
}

dd($_FILES);
array:1 [
   "attachment" => array:5 [
     "name" => "Presentation - José Victor_20240125_070758_0000.pdf"
     "type" => ""
     "tmp_name" => ""
     "error" => 1
     "size" => 0
   ]
]

Here is also the form I am using to send the file:

<form action="http://localhost:8080/app/public/..." method="POST" enctype="multipart/form-data">
     <input type="file" class="form-control" name="attachment" required="">
     ...
</form>

My only question is: WHAT NOW? WHAT IS PHP'S CRITERIA FOR READING A FILE AND NOT READING ANOTHER?

Is there a PHP configuration file where I limit something like this?