Trying to validate an uploaded file in CakePHP 5
$validator
->notEmptyFile('thumbnail_upload', 'an image is required')
->requirePresence('thumbnail_upload', 'create')
->add('thumbnail_upload', [
'mimeType' => [
'rule' => [
'mimeType', [
'image/jpeg',
'image/png',
'image/jpg'
],
],
'message' => 'File type must be either .jpg, .jpeg or .png'
],
]);
And whilst the files in question are valid, this step in the validator always fails. There's no errors with the \Laminas\Diactoros\UploadedFile or anything else, it's as far as I can tell the validator. This is the file upload form input
<?= $this->Form->control('image_upload', [
'type' => 'file',
'accept' => 'image/jpeg, image/jpg, image/png'
]) ?>
Completely stuck and not sure why, I've tried using the uploadedFile() validation method but that still gets the error.
The issue was I was moving the file before validating the entity. This caused the moved boolean to change to true, which was causing the fail. Commented out code is where I originally had it, below the patch is where it needed to be.
I was originally confused as I had an almost identical thing somewhere else, however that was uploading things to an S3 bucket, so in the eyes of Laminas\Diactoros\UploadedFile, this wasn't being moved, and so passed validation.