4

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

1 Answer 1

7

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'
    ));
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that helped alot, although now it is failing at the validation. If I run file --mime-type filename it returns me the same mime type I placed on the validate however it is failing the validation and not allowing to proceed... am I doing something wrong on the code?
I updated my answer, I think you want mimetypes validation rule.

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.