t0berius
1 month ago

laravel validate array of images

Posted 1 month ago by t0berius

Is there a way to validate uploaded images (uploaded as array) inside the controller without creating an extra FormRequest?

My upload input looks like this:

<input type="file" accept="image/x-png,image/jpg,image/jpeg" class="custom-file-input {{ $errors->has('productImage.0') ? 'is-invalid' : '' }}" id="productImage[]" name="productImage">

This input element is displayed multiple times for sure, so a user can upload 4 images per product.

Is there any way I can validate the "array" of images at once?

I tried:

    $request->validate([
        'productImage.*' => 'required|size:10',
    ]);

Which doesn't work, using:

    $request->validate([
        'productImage.0' => 'required|size:10',
    ]);

Was working, but somehow I don't want to specify every image per line, is there a way to validate them as an array with a maximum of 4 images to be validated?

Please sign in or create an account to participate in this conversation.