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

2 Answers 2

2

Try something like this:

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

Based on the answer here

Use the validator from here

Sign up to request clarification or add additional context in comments.

2 Comments

Didn't work. Method [validateIntegerOrArray] does not exist. I guess I will have to extend the validation class or make my own rule.
Yes, use the validator extended in the question of the link I provided. See my edited answer
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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.