kfirba's avatar
Level 50

validating uploaded file always fails

Hey.

I got a form that have an input for a file. When I submit the form, I validate that the a photo was uploaded by using the following rule:

'image' => 'required|mimes:jpg,jpeg,png,bmp'

My input field code is:

<div class="form-group row{{ $errors->has('image') ? ' has-danger' : '' }}">
                <label for="photo" class="col-sm-2 col-form-label"><strong>Image</strong></label>
                <div class="col-sm-10">
                    <input id="photo" class="form-control-file" type="file" name="image">

                    @if ($errors->has('image'))
                        <div class="form-control-feedback">
                            <strong>{{ $errors->first('image') }}</strong>
                        </div>
                    @endif
                </div>
 </div>                 

The form contains the enctype="multipart/form-data" attribute so it should upload the file.

90% of the times when I submit the form, I get the error validation message The photo field is required.. The other 10% of the times it somehow works... I really don't know what's going on..

I tried to dd($request->file('photo')) and I get an instance of UploadedFile. Why validation fails then?

Does anyone have an idea what's going on here?

0 likes
8 replies
minjon's avatar

@kfirba Your input name = "image", and you validate "image". So I suppose you should try dd($request->file('image')), and get image field is required in the message.

kfirba's avatar
Level 50

@minjon I'm using laravel built-in validation rules. It should work automatically on any request thus the rules I've specified should suffice.

I've managed to narrow the issue down to some photos. I think there might be an issue with Laravel Valet. Maybe Caddy only accepts files that are up to X MB (trying to figure out what X is).

minjon's avatar

@kfirba I'm using another server, but I suppose there must be a php folder and php.ini in it. By default upload_max_filesize = 2M, but you can customize it. You can check the default value if you run phpinfo().

P.S. I must be missing sthg about how the Laravel works. :( I'm also using built-in validation rules, and with me Laravel picks up the names of the input fields. So, for example if my input name = image, and 'image' => 'required', then I get the message 'The image field is required'. And I also have to ask for $request->file('image'), to get the output.

I am wondering where does the Laravel get the name 'The photo field is required' if your input name = 'image' ? And why $request->file( 'photo')?

jekinney's avatar

File upload size is also set in php's Ini file too. Test with a small image like a thumbnail size that's only a few mb in size. If it works well then your probably on the right track something is limiting your file size.

Though the statement "photo is required" doesn't make sense for a image variable failure unless you have a custom error message.

kfirba's avatar
Level 50

@minhon @jekinney well.. The issue is with the file size. When it's over 2MB the webserver just ignores it and removes that from the input instead of throwing an exception or something. I wonder why there wasn't even a notice about this..

ccsl's avatar

I have the same error, in my pc the max upload size is 40MB, but I'm still getting this error, and it is only getting for png images. but I have written rules for upload png images.

'imgfile' => 'required|mimes:jpg,bmp,png'

Tray2's avatar

@ccsl Open your own thread instead of highjacking an old thread. It increases the chance of getting help.

1 like
Online-Marketing's avatar

For all who always get validation issues for their image upload and came across this thread:

Make sure you add enctype="multipart/form-data" to your form.

<form action="{{ route('profile.update') }}" method="POST" enctype="multipart/form-data">
3 likes

Please or to participate in this conversation.