upload multiple files at once and validate file mimes on front / back
In order to upload several files at once we need to specify
multiple
attribute and append
[]
to input name
<input type="file" multiple="multiple" name="files[]" placeholder="Attach file"/>
To validate show errors on client side we have to append input name with
.*
Complete example of the
form-group
<div class="form-group">
    <label for="files">Attach File</label> &nbsp;
    <i id="add_new_file" class="ki ki-plus text-success pointer" title="Add New File"></i>
    <input type="file" class="form-control mb-2 {{ $errors->has('files.*') ? 'is-invalid' : '' }}" multiple="multiple" name="files[]" placeholder="Attach file"/>
    @if($errors->has('files.*'))
        <div class="invalid-feedback">
            <strong>{{ $errors->first('files.*') }}</strong>
        </div>
    @endif
</div>
Server side validation of mime types
public function rules()
{
    return [
        'comment' => 'sometimes|nullable|min:2|max:255',
        'files.*' => 'sometimes|nullable|mimes:pdf,docx,xlsx,pptx,rar,zip,png,jpg',
    ];
}
It's necessary the validator key
files
to be appended with the same symbols
.*
by Valeri Tandilashvili
4 years ago
Laravel
validation
3
Pro tip: use ```triple backticks around text``` to write in code fences