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.
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';
}
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
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.
or use this :)
@if ($errors->has('images.0'))
The images must be an image.
@endif
What if you have like 50 images? You can't do that 50 times right?
@rikarsen *images.0* could be good but not images.1 or another one.
@blackbird It is not repeated..
@bestmomo you're right
@if ($errors->has('images.1'))
The files must be an image.
@elseif ($errors->has('images.0'))
The file must be an image.
@endif
Please sign in or create an account to participate in this conversation.