I'm creating a form in Laravel 5 which has a product name, product main image and secondary image. Both the image field and secondary field are optional.
<div class="form-group {{ ($errors->has('title')) ? 'has-error' : '' }}">
<label>Title *</label>
<input class="form-control validate[required]" name="title" type="text" value="{{ Input::old('title') }}">
{{ ($errors->has('title') ? $errors->first('title') : '') }}
</div>
<div class="form-group">
<label for="featured_image">Cover Image
<small>(optional)</small>
</label>
<input type="file" placeholder="" id="featured_image" name="featured_image">
<small class="description"> Maximum file size: 2 MB.</small>
{{ ($errors->has('title') ? $errors->first('featured_image') : '') }}
</div>
<div class="form-group">
<label for="gallery_images">Gallery Images
<small>(optional)</small>
</label>
<input type="file" placeholder="" id="gallery_images" name="gallery_images[]" multiple="">
<small class="description">Maximum file size: 2 MB.</small>
{{ ($errors->has('title') ? $errors->first('gallery_images') : '') }}
</div>
My validation in the request file is:
public function rules()
{
return [
'title' => 'required|min:5',
'featured_image' => 'mimes:jpeg,png,jpg|max:2048',
'gallery_images' => 'mimes:jpeg,png,jpg|max:2048'
];
}
But it always checks for the image upload whether it is uploaded or not. Which is incorrect, the image type was checked only when images were upload else not.
Thank you.