11

My application allows the user to upload multiple image files at the same time, however I can't figure out how to validate the array of images.

$input = Request::all();

    $rules = array(
        ...
        'image' => 'required|image'
    );

    $validator = Validator::make($input, $rules);

    if ($validator->fails()) {

        $messages = $validator->messages();

        return Redirect::to('venue-add')
            ->withErrors($messages);

    } else { ...

This validation will fail, as 'image' is an array, if I change the validation rule to:

    $rules = array(
        ...
        'image' => 'required|array'
    );

The validation will pass, but the images inside of the array haven't been verified.

This answer uses the keyword each to prefix validation rules, however this is in laravel 4.2 and in Laravel 5 it doesn't seem to work.

I've been trying to iterate through the array and validation on each image individually, but is there a built in function to do this for me?

Community
  • 1
  • 1
TimothyBuktu
  • 2,016
  • 5
  • 21
  • 35

4 Answers4

28

This works for me

$rules = array(
     ...
     'image' => 'required',
     'image.*' => 'image|mimes:jpg,jpeg'
);

Do it in that order.

Badmus Taofeeq
  • 761
  • 9
  • 18
7

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);

    }
}
TimothyBuktu
  • 2,016
  • 5
  • 21
  • 35
5

Agree with @Badmus Taofeeq answer but it will accept image field with any value without check field is array or not. you need to add small thing

Refer below code for proper validation

$rules = array(
     'image' => 'required|array',
     'image.*' => 'image|mimes:jpg,jpeg'
);
Jatin Kaklotar
  • 445
  • 5
  • 17
0

This works for me. In ProductRequest.php File:

'product_images' => 'required|array',
'product_images.*' => 'image|mimes:jpg,jpeg'
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Ethan Jun 10 '22 at 01:08