validation images

Published 3 months ago by LSheNNawY

Hi there, I'm trying to validate two files inputs one for a logo and the other for an icon,

1- added the enctype="multipart/form-data" to the form.

2- validation rules for these inputs are:[ 'logo' => 'image|mimes:jpeg, jpg, bmp, png', 'icon'=> 'image|mimes:ico', ]

3- when I try to upload a logo of the type png, it returns an error says "The Logo must be a file of type: jpeg, jpg, bmp, png."

4- when I try to upload an icon of the type ico it returns error says "The Icon must be an image."

Best Answer (As Selected By LSheNNawY)
Cronix

Try removing the spaces between the mime types in both places

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

When a rule accepts parameters separated by commas, there can't be any spaces in between the values since it will explode() on commas to get the individual parameters and if they have spaces in it then it becomes " jpg" and " png" instead of "jpg" and "png". I don't think it runs trim() on the results.

Notice in the user guide how they don't have spaces: https://laravel.com/docs/5.6/validation#rule-mimes

'photo' => 'mimes:jpeg,bmp,png'

Cronix
Cronix
3 months ago (568,610 XP)

Try removing the spaces between the mime types in both places

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

When a rule accepts parameters separated by commas, there can't be any spaces in between the values since it will explode() on commas to get the individual parameters and if they have spaces in it then it becomes " jpg" and " png" instead of "jpg" and "png". I don't think it runs trim() on the results.

Notice in the user guide how they don't have spaces: https://laravel.com/docs/5.6/validation#rule-mimes

'photo' => 'mimes:jpeg,bmp,png'

Cronix
Cronix
3 months ago (568,610 XP)

But I don't know why you'd have both image and mimes in the same rule since your mime types are already covered by the image rule.

bobbybouwmann

Your mimes list should be without spaces as far as I know!

Also for images there is a separate validation rule called image, you don't need mimes anymore then ;)

Documentation: https://laravel.com/docs/5.6/validation#rule-image

The ico rule should work as far as I know! Are you sure that image is of the type image/x-icon?

LSheNNawY

Thanks you all, the problem was the spaces and the ico file only worked when i removed image rule @bobbybouwmann from icon attribute

Please sign in or create an account to participate in this conversation.