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.
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
sizeand other values)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)
Here is also the form I am using to send the file:
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?