cherrycoding
cherrycoding

Reputation: 1895

Multiple file upload and validation

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

Upvotes: 2

Views: 1447

Answers (1)

Varun Davda
Varun Davda

Reputation: 31

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;

}

Upvotes: 1

Related Questions