Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate mime for multiple files upload in laravel 5?

Tags:

php

laravel

I got a form which sends many input fields to be validated into a controller in Laravel 5, and among them there is the files name="arquivos[]" <input> field. I want to solve all the validantion in a single step as I was doing but it seems that it is failing by the fact that it is receiving and array of files and not a single one. The code of the form is:

{!! Form::open(['route' => 'posts.store', 'enctype' => 'multipart/form-data']) !!}
...other inputs
{{  Form::file('arquivos[]', array('class' => 'novo-post-form-item', 'required'=>'','multiple' => '')) }}
{!! Form::close() !!}

And in my posts.store function:

$this->validate($request, array(
        'arquivos' => 'required|mimes:image/jpeg,image/png,image/gif,video/webm,video/mp4,audio/mpeg',
        'assunto' => 'max:255',
        'conteudo' => 'required|max:65535'
    ));

note: I don't know why in the validator I must specify the input name without the [] but it works that way...

This question is similar to some I found here at stack overflow but this time I'm asking if there is a solution for Laravel 5. As it seems, this validate methos is expecting a single file input field. Thanks in advance

like image 746
Fnr Avatar asked Sep 05 '25 16:09

Fnr


1 Answers

Please read the Laravel documentation thoroughly. It's really helpful

To validate array field, you want this: https://laravel.com/docs/5.4/validation#validating-arrays.

Also I think you want to use mimetypes validation rule because mimes validation rule accepts file extensions as parameter: https://laravel.com/docs/5.4/validation#rule-mimetypes

And the solution to your problem:

$this->validate($request, array(
        'arquivos.*' => 'required|mimetypes:image/jpeg,image/png,image/gif,video/webm,video/mp4,audio/mpeg',
        'assunto' => 'max:255',
        'conteudo' => 'required|max:65535'
    ));
like image 65
aceraven777 Avatar answered Sep 08 '25 12:09

aceraven777