0

I have a form like this that can have multiples images[] field:

{{ Form::open(array('url' => 'foo/bar', 'files' => true)) }}
    <input type="file" name="images[]">
{{ Form::close() }}

How can I validate this field with laravel 4 validation? I tried this rule but it didn't work:

$rules = array('images[]' => 'required|image');
Antonio Carlos Ribeiro
  • 86,191
  • 22
  • 213
  • 204
Diego Castro
  • 2,046
  • 2
  • 21
  • 28

2 Answers2

2

Try something like this:

$rules = array(
    'images'     => 'required|min:1|integerOrArray'
);

Based on the answer here

Use the validator from here

Community
  • 1
  • 1
Glad To Help
  • 5,299
  • 4
  • 38
  • 56
1

First of all are you running "images[]" over a loop? There is plenty of code examples for laravel 4 that actually don't work at all... And people who wrote them don't know how to use var_dump()...

If not - your probably running validation over a array of objects, try check:

var_dump(Input::file('files'));

So now after this lecture, lets run a for loop (had some issue with foreach idk why):

$files = Input::file('images');
$filesCount = count($files);

for ($i=0; $i < $filesCount; $i++)
{
    $file = $files[$i];
    $input = array(
        'upload' => $files[$i]
    );

    $rules = array(
        'upload' => 'image|max:15500'
    );
    $validation = Validator::make($input, $rules);
}

And now you can run your validation. Laravel4 only tries to valid mime type... so its vulnerable.

  • I thought there was a rule that would do that to me, apparently there is not. I do not like the idea or run the validation on loop. Like I commented, it seems the only way is to either extend the validation class or write my own rule. Thanks for your answer. – Diego Castro Oct 11 '13 at 14:39