rikarsen's avatar

Multiple Image upload validation

Hi. I have a multiple file upload input here:

{!! Form::file('images[]', ['multiple' => 'multiple', 'required' => 'required']) !!}
<span class="help-block">{{ $errors->first('images') }}</span>

And i go to validate here in request:

protected $dontFlash = ['images'];

return [
    'images' => 'required|image'
];

But in any case it returns The images must be an image.

0 likes
8 replies
bestmomo's avatar

Hi,

Did you set 'files' => true in form ?

On the other hand as you have an array you need to do something like this in form resquest :

$nbr = count($this->input('images')) - 1;
foreach(range(0, $nbr) as $index) {
    $rules['images.' . $index] = 'required|image';
}
1 like
rikarsen's avatar
rikarsen
OP
Best Answer
Level 1

thanks @bestmomo ... in this forked

$imagesArray = [];

$nbr = count($this->file('images')) - 1;

foreach(range(0, $nbr) as $index) {
    $imagesArray['images.' . $index] = 'required|image';
}

return $imagesArray;

but then I do not know how to display errors

bestmomo's avatar

I always use Ajax when I have array inputs (and dynamic controls) to better manage it. In your case you get errors with keys "images.0", "images.1"... So with Ajax it's easy to put good error message at the good place on page.

1 like
bobbybouwmann's avatar

What if you have like 50 images? You can't do that 50 times right?

Please or to participate in this conversation.