nine's avatar
Level 1

form validation

Hi,

either i put my code validation in controller or in form request , rules i put does not recognize type of file i'm trying to upload and display this message :

--> The filename must be a file of type: image/jpg.

here is my rule, simple :

return [
            'filename' => 'required|mimetypes:image/jpg',
        ];

i also try with extension : mimes:jpg,jpeg,png ..... no success ..

and here is my form

<form method="post" action="{{url('create')}}" enctype="multipart/form-data">
        
        @csrf
        <div class="row">
          <div class="form-group col-md-4">
            <input type="file" name="filename[]" class="form-control" multiple="multiple">
          </div>
        </div>
....

some idea ? thank you !

0 likes
4 replies
Cruorzy's avatar

Been a while for me but it seems like you can upload multiple files and your validation expect a single file, unless you foreach en then check the validation but I don't think so.

I'm not sure if the following thing is even possible but maybe...

return [
     '*.filename' => 'required|mimetypes:image/jpg',
];
Snapey's avatar

remove the brackets after name="filename[]". currently it will be passed as an array (which is not jpg)

or, if you want to allow multiples, you have to validate filename.*

nine's avatar
Level 1

filename.* do the job

i need to keep brackets (multiple upload)

have a good day and thank you @Snapey ...

Please or to participate in this conversation.