Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mimes Validation not working correctly in Laravel

My webpage contains a file for upload, and I want the file uploaded to only be either pdf, doc or docx.

My form tag also has enctype="multipart/form-data"

My html looks like:

<div id="cv_upload" class="row">
    <div class="col-xs-12">
          <input type="file" name='cv'>
    </div>
</div>

The $rules array associated with this is as follows:

'cv' => 'mimes:application/pdf,application/doc,application/docx|required'

And finally my messages looks like:

'cv.required' => 'A selection for C.V. is required before proceeding.',
'cv.mimes' => 'CV must be of the following file type: pdf, doc or docx.'

The only problem with this is, even after I upload a pdf or doc, the message I receive is the one for required. I have no idea why this isn't working as expected. I also tried removing the 'application/' but that yields no success either. Please help.

like image 996
Muhammad Avatar asked Jun 22 '17 14:06

Muhammad


People also ask

How does laravel validation work?

The validate method accepts an incoming HTTP request and a set of validation rules. If the validation rules pass, your code will keep executing normally; however, if validation fails, an exception will be thrown and the proper error response will automatically be sent back to the user.

What is confirmed validation in laravel?

confirmed. The field under validation must have a matching field of foo_confirmation . For example, if the field under validation is password , a matching password_confirmation field must be present in the input.


1 Answers

Make sure your form has an attribute enctype="multipart/form-data"

Also, if you use mimes validation the format must be 'mimes:jpeg,bmp,png' https://laravel.com/docs/5.6/validation#rule-mimes

You can read about the enctype attribute here : What does enctype='multipart/form-data' mean?

like image 127
Nel Avatar answered Oct 11 '22 15:10

Nel