2

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,

4 Answers 4

4

You can use check for the images.

$errors->has('file.*')
Sign up to request clarification or add additional context in comments.

2 Comments

I've already tried this. This giving error Undefined offset: 0
3

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,

Comments

1

Try like this to validate

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

5 Comments

validation has no problem. I've problem with error messages
Once you use this answer, just print whole $errors to see the messages, so you will get the idea.
When I print all errors there is validation error but how to pick that error over there
Just Check anything your get when using @if ($errors->has('file.0')) <span class="help-block"> {{ $errors->first('file.0') }} </span> @endif
1

If anyone is still struggling with multiple file upload issues, here is a working solution for this:

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>

And if you are looking to show all the errors in blade view, (for all files being uploaded) you could consider using something like this in your blade view:

@if ($errors->any())         
  <div class="invalid-feedback d-block" role="alert">
    <ul>                        
      @foreach (json_decode($errors) as $file_index => $error)
       <li>
         <strong>{{$file_index.': '}} 
           <ul>
              @foreach ($error as $error_item)
                <li>{{$error_item}}</li>
              @endforeach
           </ul>
         </strong>
        </li>
       @endforeach
     </ul>
   </div>
@endif

1 Comment

But why is that the correct way? Could you consider adding details? :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.