Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image array validation in Laravel 5

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?

like image 201
TimothyBuktu Avatar asked Jul 21 '15 08:07

TimothyBuktu


1 Answers

This works for me

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

Do it in that order.

like image 81
Badmus Taofeeq Avatar answered Sep 21 '22 14:09

Badmus Taofeeq