3

I have multiple input fields with multiple file upload for each field.

<?php for ($i = 0; $i < $total; $i++)
   {
?>
<div class="col-md-3 addedClass">
     <label>Vehicle Images</label>
     <input type="file" name="vehicle_image[{{$i}}][]" multiple="multiple">
     @if($errors->has('vehicle_image'))
          <span class="help-block">
              <strong>{{$errors->first('vehicle_image')}}</strong>
          </span>
     @endif
</div>
<?php } ?>

I have got files in the request like this:

"vehicle_image" => array:2 [▼
    0 => array:2 [▼
      0 => "citizenship.jpg"
      1 => "logo_vehicle.png"
    ]
    1 => array:2 [▼
      0 => "ae backend.jpg"
      1 => "logo_vehicle.png"
    ]
  ]

In this case, I have two input fields with 2/2 files. When I have tried to validate mime type for only images like this:

$this->validate($request,[
           'vehicle_image' => 'mimes:jpeg,png,bmp,tiff |max:4096'
       ],$messages[
            // error messages 
      ]);

I have got following error:

FatalThrowableError in ReservationController.php line 67: Cannot use [] for reading

Can someone tell me what is wrong with the above code ? Suggestions are appreciated.

3
  • What if you remove the blank $messages array Commented Jun 5, 2017 at 5:41
  • $this->validate($request,['vehicle_image' => 'mimes:mimes:jpeg,png,bmp,tiff |max:4096']); Commented Jun 5, 2017 at 5:41
  • I don't have that in my code. This is error while asking question Commented Jun 5, 2017 at 5:44

2 Answers 2

3

Try removing the blank $messages array and calling the function all() for the inputs on the request.

$rules = [
    'vehicle_image' => 'mimes:jpeg,png,bmp,tiff |max:4096'
];
$this->validate($request,$rules);

To display the default error message you would throw an exception using something like:

    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );
        return redirect('VIEWPATH.VIEWNAME')->withErrors($validator)->withInput();
    }

Then to display the errors you can have something like the following in your blade view or template:

@if (count($errors) > 0)
  <div class="alert alert-danger alert-dismissable fade in">
    <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
    <h4>
      <i class="icon fa fa-warning fa-fw" aria-hidden="true"></i>
      <strong>Error!</strong> See error messages...
    </h4>
    <ul>
      @foreach ($errors->all() as $error)
        <li>{{ $error }}</li>
      @endforeach
    </ul>
  </div>
@endif

or

@if(session()->has('errors'))
    <div class="alert alert-danger fade in">
        <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
        <h4>Following errors occurred:</h4>
        <ul>
            @foreach($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
Sign up to request clarification or add additional context in comments.

9 Comments

Let me try this one
Just curious is this blank array here intentional? vehicle_image[{{$i}}][]
Yes, because of multiple files uploaded through every input field
in that case you may need to validate using a custom rule that loops through the inputs array and validates those.
same concept with another loop
|
0

just remove extra empty array index from the input field.

<input type="file" name="vehicle_image[{{$i}}]" multiple="multiple">



$this->validate($request, [
            'vehicle_image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:4096'
        ]);

and dd($errors) see result

Comments

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.