Validating multiple files in array
You can validate files array like any input array in Laravel 5.2. This feature is new in Laravel 5.2.
$input_data = $request->all();
$validator = Validator::make(
$input_data, [
'image_file.*' => 'required|mimes:jpg,jpeg,png,bmp|max:20000'
],[
'image_file.*.required' => 'Please upload an image',
'image_file.*.mimes' => 'Only jpeg,png and bmp images are allowed',
'image_file.*.max' => 'Sorry! Maximum allowed size for an image is 20MB',
]
);
if ($validator->fails()) {
// Validation error..
}
Please try this:
public function fileUpload(Request $request) {
$rules = [];
$files = count($this->input('files')) - 1;
foreach(range(0, $files) as $index) {
$rules['files.' . $index] = 'required|mimes:png,jpeg,jpg,gif|max:2048';
}
$validator = Validator::make($request->all() , $rules);
if ($validator->fails()) {
return response()->json(array(
'success' => false,
'errors' => $validator->getMessageBag()->toArray()
) , 400);
}
}