So in general I would recommend to keep these two validation separated. This way you can directory validate the files the way you want. Note that this requires some extra work. You have a few options here
- Create your own custom validation rule. You can then check if the given file is an image and handle the validation accordingly and also for the files.
Documentation: https://laravel.com/docs/5.6/validation#custom-validation-rules
- Use different validation rules based on the given input. So for example you check the mime type before you go into validation. So you might have code that looks like this
$file = $request->get('file');
$mimeType = $file->getMimeType();
$rules = [
'file' => 'required|mimes:xls,xlsx,pdf|max:2048',
];
if (in_array($mimeType, $imageMimeTypes)) {
$rules = [
'file' => 'required|image|max:2048|dimensions:min_width=500,min_height=500,ratio=3/2',
];
}
$validator = Validator::make($request->all(), $rules);
- You can also do this on the client side. Instead of always posting to
fileyou can decide to post with a key calledfileor a key calledimage. Each can then have their own validation rule. If you usesometimesit will only validate it when the key is available in the request.
Documentation: https://laravel.com/docs/5.6/validation#conditionally-adding-rules