简体   繁体   中英

Laravel multiple file upload validation

I am currently working in a form.

I have some issue with multiple file upload validation. I have only one field in a form which allows multiple file upload.

<input type="file" name="file[]" multiple="multiple">

And this is my validation,

$this->validate($request, [
    'file' =>'required',
    'file.*' => 'required|mimes:pdf,jpeg,png |max:4096',
],
 $messages = [
            'mimes' => 'Only PDF, JPEG, PNG are allowed.'
        ]
);

The validation works perfectly but I am not able to display error messages in blade file.

Here are my attempts.

@if($errors->has('file'))
    <span class="help-block">
        <strong>{{$errors->first('file')}}</strong>
    </span>
@endif

This is for displaying error if no file is uploaded.

Suppose I have uploaded following files,

abc.jpg
abc.html
abc.pdf

When mimes type validation throws error I am not able to display error message. Here in this case, error is thrown as $error->first(file.1) since validation fails at index 1

This index can be any index according to files uploaded and $error->first(file.*) doesn't work as well.

When I display all error after adding invalid files only from form, I've got these errors.

 Only PDF, JPEG, PNG are allowed.
 The type field is required. 
 The number field is required.
 The expiry date field is required. 

Any one have idea about this. Any help is appreciated.

Thanks,

您可以使用图像检查。

$errors->has('file.*')

像这样尝试验证

'file.*.mimes' => 'Only PDF, JPEG, PNG are allowed.',

This is not the good way, but it's fine for my case.

I have validation rules

'file' =>'required',
'file.*' => 'required|mimes:pdf,jpeg,png |max:4096',

And, Error message

'file.*' => 'Only PDF, JPEG, PNG are allowed.'

Since I have only one file upload field, I have just checked this error message in a list of all messages and then displayed as follows.

<input type="file" name="file[]" multiple="multiple">
@foreach($errors->all() as $error)
    @if($error=="Only PDF, JPEG, PNG are allowed.")
            <span class="help-block"><strong>{{$error}}</strong></span>
    @endif
@endforeach

Thanks to all guys,

If anyone is still struggling with multiple file upload issues, here is the correct way to do it.

Validation Rules in Controller

    $validated = $request->validate([            
        'files.*' => 'mimes:pdf,jpeg,png|max:4096',
        'files' => 'required',
    ], 
    $messages = [
        "files.required" => "You must upload atleast one file",                      
        "files.*.mimes" => "This file type is not allowed", 
        "files.*.max" => "Max file size is 4Mb",
    ]);

And in blade view, display the errors like so

 <form action="{{ url('/multifile') }}" method="POST" enctype="multipart/form- data"> @csrf <div class="form-group"> <label>Select files</label> <input type="file" class="form-control-file @error('files') 'is-invalid' @enderror" name="files[]" multiple> @error('files') <span class="invalid-feedback d-block" role="alert"> <strong>{{ $message }}</strong> </span> @enderror @error('files.*') <span class="invalid-feedback d-block" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> <button class="btn btn-secondary" type="submit">Submit</button> </form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM