I have a form that allows multiple files to be attached.
I am validating the form for attachment field as:
$this->validate($request, [
'attachments.*' => 'mimes:jpg,jpeg,bmp,png|max:5000',
]);
It works properly but I also want to only allow maximum of 3 files to be uploaded at a time.
How do I achieve this ?
As attachments
is an array, you can use max
rule to validate it max elements as 3
$messages = [
"attachments.max" => "file can't be more than 3."
];
$this->validate($request, [
'attachments.*' => 'mimes:jpg,jpeg,bmp,png|max:5000',
'attachments' => 'max:3',
],$messages);
There is option to add Custom Validation Rules in laravel.
Also you can try something like this:
$this->validate($request, [
'attachments.*' => [
'mimes:jpg,jpeg,bmp,png',
function($attribute, $value, $fail) {
if (count($value) > 3) {
return $fail($attribute . ' should be less than or equal to 3.');
}
},
]
]);
There is another solution posted here: How to validation number of files
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With