samedi 31 octobre 2015

Laravel 5 : check image type validation only if uploaded

I am creating a form in laraval 5 which has a product name, product main image image and secondary image. Both the image field are optional, here is my input fields

            <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>

and my validation in 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 check 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.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire