Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.5 Validate multiple file upload

How do I validate multiple file uploads using only one validation on laravel?

$this->validate($request, [
    'project'           => 'required|exists:project_details,id',
    'location'          => 'exists:project_details,location',
    'plant_id'          => 'exists:project_details,plant_id',
    'capacity'          => 'required|max:20',
    'brief_description' => 'nullable|max:300',
    'incident_details'  => 'required|max:300',
    'other_comments'    => 'nullable|max:300',
    'attachments.*'     => 'required|file|mimes:xlsx,xls,csv,jpg,jpeg,png,bmp,doc,docx,pdf,tif,tiff'
]);

I'm trying to validate the attachments. Here's my form:

<input type="file" name="attachments[]" multiple>
like image 980
Ralph Vitto Avatar asked Mar 06 '23 23:03

Ralph Vitto


1 Answers

You can validate your file following way.

$input_data = $request->all();

$validator = Validator::make(
$input_data, [
'image_file.*' => 'required|file|mimes:xlsx,xls,csv,jpg,jpeg,png,bmp,doc,docx,pdf,tif,tiff'
],[
    'image_file.*.required' => 'Please upload an image',
    'image_file.*.mimes' => 'Only xlsx,xls,csv,jpg,jpeg,png,bmp,doc,docx,pdf,tif,tiff images are allowed',

]
);

if ($validator->fails()) {
    $messages = $validator->messages();
    return Redirect::to('/')->with('message', 'Your erorr message');
}
like image 176
Amit Senjaliya Avatar answered Mar 12 '23 10:03

Amit Senjaliya