movepixels's avatar

Laravel 6 image validation failure

I just recently upgraded an existing app to Laravel 6 and testing everything out and my image upload validation now fails.

Its currently working fine on live production site, zero changes to existing controller and formRequest but getting error => The media must be an image

I am uploading an image and the formRequest rules are:

public function rules()
{
    return [
      'media' => [
        'required',
        'image',
        'max:3072',
        'mimes:jpeg,png,jpg',
        Rule::dimensions()->minWidth(700)->minHeight(500)
      ],
    ];
  }

So it worked in 5.8 but fails in 6.0

If I simply by-pass the formRequest and skip that step the controller handles the upload with no errors so the upload functionality / saving / resizing all works fine.

Its simply when passing the request thru the formRequest it fails. If I remove "image" from rules array Laravel detects the file is or is not an specific mime type, size and dimensions but will fail if the image rule is added.

Any ideas?

0 likes
8 replies
movepixels's avatar

Correct on the redundancy fact, I was just wondering if there was a specific failure based on the code. Just because it is as you stated it redundant, but still so it worked unchanged in 5.8 then fails in 6.0 yet simply putting the redundancy fact aside and possibly being sloppy code, it should work. If I remove the mimes type it still tells me my jpg, or png is invalid and not an image.

return [
      'media' => [
        'required',
        'image'
      ],
    ];

Same error Is not an image.

Proper coding format:

return [
      'media' => 'required|image'
];

Same error still telling me my jpg image is not an image

newbie360's avatar

change the #uploadFile

<input type="file" id="uploadFile" ......>

and put this script in the same page, did you see a popup 'image/......' ?

<script>
    $("body").on("change", "#uploadFile", function(e) {
        alert(e.target.files[0].type);
    });
</script>
1 like
movepixels's avatar

not using jQuery or anything like that, but still should not make any difference since if I remove image from the rules or skip the formRequest it works 100% and the 5.8 version works fine, 6.0 untouched fails so has noting to do with frontEnd since Laravel is simply an API backend so the data being sent has not changed, same format, same form sending the data so thats not the issue, but thanks all the same for your time

movepixels's avatar

For the sake of it this is the data / log of the info before the rules() in the formRequest

array (
  'media' => 
  Illuminate\Http\UploadedFile::__set_state(array(
     'test' => false,
     'originalName' => '17033.jpg',
     'mimeType' => 'image/jpeg',
     'error' => 0,
     'hashName' => NULL,
  )),
)  

The front-end completely separate and is working 100% on live site using laravel 5.8 API backend and its been running for 2 years with zero issues. Its only now after upgrading to 6.0 are the uploads no longer accepting the image rule. I upload the exact same image over and over, it will upload if i remove "image" from the rules, tells me its not an image when its added back in. Its clearly an image. The front-end has file validation in place also so if i select a pdf, doc, anything other than an image the upload will not process and will not even attempt to send the file to the server unless its a valid image format. Its only when image rule is left inplace does it now fail.

I will simply stick with mimes array and consider this closed because its rather foolish to investigate this any further

movepixels's avatar

Oddly enough it turns out my version is different.

On my v 6.0.4 some of the types are missing....

My source code looks like:

public function validateImage($attribute, $value)
{
        return $this->validateMimes($attribute, $value, ['jpeg', 'png', 'gif', 'bmp', 'svg', 'webp']);
}

while original GitHub source has the 'jpg' type included

public function validateImage($attribute, $value)
{
        return $this->validateMimes($attribute, $value, ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'webp']);
}

Good looking out!

2 likes

Please or to participate in this conversation.