2

I want to allow multiple files upload, so I did this in my view:

{{ Form::file('files[]', array('multiple'=>true)); }}

it works, but I can't validate it. For testing purposes I've created this rule:

'files' => 'required|mimes:png,jpg,jpeg'

but it doesn't work, it always says the mime type is incorrect, I also tried it with png. Can anyone help me please? I also tried to remove the [] from the file input name. Could be the problem that laravel doesn't support multiple files at validation?

Thanks

8
  • 2
    MIME types have a different form, like this: image/png, image/jpeg... So you're probably specifying wrong MIME types. Commented Jul 31, 2014 at 8:02
  • I can't give an answer as my knowledge isn't too good here but first off try changing your validation to required|image rather than specifying the mime types of images. (FWIW one of my team has definitely had issues in the past using the mimes rule as you are where using image fixed it.) Also there is a validation rule array that ensures the value is an array. However, I don't know if you can then set rule against the actual elements of the array. The documentation is pretty useless here. Commented Jul 31, 2014 at 8:03
  • I've never worked with Laravel as well, but that's what first came to me when I read your question. You might be interested in this: stackoverflow.com/questions/19849702/laravel-mime-validation. So this question might be a duplicate of the above-linked one. Commented Jul 31, 2014 at 8:06
  • I've also tried it with a validation rule like this: 'files' => 'required|mimes:image/jpg,image/jpeg|max:20000' but it still not work. Commented Jul 31, 2014 at 8:13
  • 1
    @user3038158 in the end you may find that you have to do a bit of manual validation here - first ensure that files validates against array then, if so, iterate over all files and validate each one individually against required|image. Then somehow bundle up all the validation errors into one MessageBag to send to the redirect if there's an error. Commented Jul 31, 2014 at 8:17

1 Answer 1

1

I had the same problem, i got the solution by editing the code of the request handler.

public function rules()

{
    $rules = [];

    $nbr = count($this->file('field_name')) - 1;

    foreach(range(0, $nbr) as $index) {

        $rules['field_name.' . $index] = 'required|mimes:jpeg,jpg,png';

    }

    return $rules;

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

Comments

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.