Image array validation in Laravel 5
This works for me
$rules = array(
...
'image' => 'required',
'image.*' => 'image|mimes:jpg,jpeg'
);
Do it in that order.
I used a technique similar to what Jeemusu recommended and after the initial validation to make sure the array of images is present, iterated through the array with a second validator, making sure each item in the array is in fact an image. Here's the code:
$input = Request::all();
$rules = array(
'name' => 'required',
'location' => 'required',
'capacity' => 'required',
'description' => 'required',
'image' => 'required|array'
);
$validator = Validator::make($input, $rules);
if ($validator->fails()) {
$messages = $validator->messages();
return Redirect::to('venue-add')
->withErrors($messages);
}
$imageRules = array(
'image' => 'image|max:2000'
);
foreach($input['image'] as $image)
{
$image = array('image' => $image);
$imageValidator = Validator::make($image, $imageRules);
if ($imageValidator->fails()) {
$messages = $imageValidator->messages();
return Redirect::to('venue-add')
->withErrors($messages);
}
}