Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Yefferson's avatar

How to display errors for multiple files array validation properly?

I've been stuck in a kind of bug for 2 days so far and I'm really desperate. I'm trying to validate a multiple input file with others fields, the validation works as expected, but the problem is when I want to know in which field the error was in order to display the proper message.

I'm using something like:

  $rules = [
    'field_1'   => 'rule',
   ....
   ....
    'field_n'   => 'rule',
   ....
    'images.*' => 'required|mimes:jpeg,bmp,png,gif|max:40000'
  ];

  $messages = [
    'field_1.required'  => 'message_for_rule'
   ....
    'field_n'   => 'rule',
   ....
    'images.*.required' => 'message_for_required',
    'images.*.mimes'    => 'message_for_mimes',
    'images.*.max'      => 'message_for_max'
  ];

  $validator = Validator::make($request->all(), $rules, $messages);

  if ($validator->fails()) {
    return Redirect::back()
                   ->withErrors($validator)
                   ->withInput();
  }

Which is the correct way to display errors for the multiple files in blade template? I hope you Could help me (I'm not used to ask for help, but I could not make it work).

0 likes
8 replies
TiborBesze's avatar

The $errors array also contains array for those fields. You should be able to access each element like this:

{{ $errors->has('images.0') ? $errors->first('images.0') }}
Yefferson's avatar

Thanks for replying so fast, @TiborBesze86 I really appreciate it. I was wondering if there a way to display them dynamically? For example:

<ul>
@if (count($errors->has('images'.*)) > 0)
    @foreach ($errors->get('images'.*) as $error)
        <li>{{ $error }}</li>
    @endforeach
@endif
</ul>
Yefferson's avatar
Yefferson
OP
Best Answer
Level 1

That's a well known error. I finished this doing a manual validation, using the php artisan make:request command and customizing the Request method.

Swaz's avatar

@Yefferson Did you ever figure out how to display the errors in a loop like that?

Yefferson's avatar

@swaz Not in a loop, but only returning a single error like: "Some field is incorrect"

rajatmasih's avatar

I figured it out you can do it simply by this

@if ($errors->has('images.*'))
      <div class="help-block">
               <ul role="alert"><li>{{ $errors->first('images.*') }}</li></ul>
        </div>
 @endif
3 likes
Hadayat's avatar

Does anyone has the idea that how to display array error in javascript because doesn't allow us to write image.0. How to display a single error.

mrdigex's avatar

@Hadayat You can loop over the errors object something like this

{errors && (
    <div className="text-red-700">
         {Object.values(errors).map((error) => {
              return error;
          })}
     </div>
)}

This is going to display all the errors if you want to display just file errors make sure to validate files in your controller later than the other fields, so you can make sure to display always just the error for files when is necessary

Please or to participate in this conversation.