Cakephp 5 file uploading validation mimeType does not work

validation set in model :-

$validator->notEmptyFile(‘featureImage’, __(‘please set feature image!’))
->add(‘featureImage’, ‘file’, [
‘rule’ => [‘mimeType’, [‘image/jpeg’, ‘image/png’]],
‘on’ => function ($context) {
return !empty($context[‘data’][‘feature_image’]);
}
]);

this code does not check mimetype

Try

        $validator
            ->allowEmptyFile('image')
            ->requirePresence('image', 'create')
            ->uploadedFile('image', [
                'types' => [
                    'image/png',
                    ‘image/jpeg’,
                ],
            ], __x('Validator', 'Allowed file extension is: .png, .jpg'));

hmmm thanks @shadowx.jb but it does not work!! please check it below

$validator->notEmptyFile(‘featureImage’, __(‘please set feature image!’))
->uploadedFile(‘featureImage’, [
‘types’ => [
‘image/jpeg’,
‘image/png’,
],
], __x(‘Validator’, ‘Allowed file extension is: .png, .jpeg’));

What does your form look like?

please check it here

Form must be

echo $this->Form->create($article, [
    'enctype' => 'multipart/form-data',
]);

not type => file

it does not matter @shadowx.jb because [‘type’=>‘file’] also generator same ectype attribute in html:-

Cakephp also mention that kind of code in Official Docs :-

Ok, sorry… try

        $validator
            ->notEmptyFile('featureImage')
            ->uploadedFile('featureImage', [
                'types' => [
                    'image/jpg',
                    'image/jpeg',
                    'image/png',
                ],
            ], __x('Validator', 'Allowed file extension is: .png, .jpeg, .jpg'));

still getting same thing bro @shadowx.jb

is featureImage set as accessible inside your Article entity class?

Your code mostly says featureImage, but your entity dump has a field in it called feature_image.

2 Likes

The featureImage object does not have to be defined in the entity class. I tried the same example and it works for me. Here is the result of $this->request->getData()

[
  'language_id' => '2',
  'user_id' => '8',
  'date' => '2024-02-16',
  'title' => 'hhh',
  'content' => '<p>hh</p>',
  'url' => '',
  'featureImage' => object(Laminas\Diactoros\UploadedFile) id:0 {
    private error => (int) 0
    private file => 'C:\wamp\tmp\php776A.tmp'
    private moved => false
    private stream => object(Laminas\Diactoros\Stream) id:1 {
      protected resource => (resource) Resource id #22
      protected stream => 'C:\wamp\tmp\php776A.tmp'
    }
    private size => (int) 112216
    private clientFilename => 'image-02.jpg'
    private clientMediaType => 'image/jpeg'
  },
]

still same response bro

You’ve shown the form and the validator config but how do you patch the request data into the entity so that the validator is being called.

I’m still coming back to the fact that the most recent entity dump that you’ve shown us has a field called feature_image (along with article_title, article_content, etc.), but the error is on featureImage. Where is feature_image coming from, and why does the entity not have a field in it called featureImage?

hi @KevinPfeifer hi @Zuluru hi @shadowx.jb thank’s guys you reply me and try to help me… i am just confused with own code :grin: actually application just working fine only my image validation getting problem… i just want to allow only image content type not file like… php or js or .exe… etc… just author upload only image…
please check it my screenshots here :-

FORM

Controller Side:-

Validation in model side:-

What does $this->request->getData() return to you in the controller?

even if I allow PNG mimetype but it still throw error… you can see in error index.
it also throw error when i upload other mimetype image like GIF… obviously throw error because i just allow only PNG JPEG JPG there in validation

Can you please insert the code as a code and not an image?
Please try to remove this from the entity featureImage => true
And please insert the entire all form code here - not the image.
I want $this->request->getData() to see this right below the POST method.

Model side validation:-

$validator->notEmptyFile('featureImage', __('please set feature image!'))
            ->add('featureImage', 'file', [
                'rule' => ['mimeType', ['image/jpeg', 'image/png']],
                'on' => function ($context) {
                    return !empty($context['data']['feature_image']);
                }
            ]);

/*  $validator
            ->notEmptyFile('featureImage')
            ->uploadedFile('featureImage', [
                'types' => [
                    'image/jpg',
                    'image/jpeg',
                    'image/png',
                ],
            ], __x('Validator', 'Allowed file extension is: .png, .jpeg, .jpg')); */

controller side method:-

public function add()
{
$article = $this->Articles->newEmptyEntity();
if ($this->request->is(‘post’)) {
if ($this->request->getData(‘featureImage’)->getSize() > 0) :
$article->feature_image = ‘images’ . DS . ‘feature-image’ . DS . $this->Zira->f

ileUploads('feature-image', $this->request);
            endif;
            //dd($this->request->getData());
            $article = $this->Articles->patchEntity($article, $this->request->getData());
            $article->user_id = $this->request->getAttribute('identity')->getIdentifier();
            //dd($article);
            if ($this->Articles->save($article)) {
                $this->Flash->success(__('The article has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The article could not be saved. Please, try again.'));
        }
        $users = $this->Articles->Users->find('list', limit: 200)->all();
        $categorys = $this->Articles->Categorys->find('list', limit: 200)->all();
        $this->set(compact('article', 'users', 'categorys'));
    }
<?= $this->Form->create($article, ['type' => "file"]) ?>
<?= $this->Form->control('featureImage',['ttype'=>"file'])?>
<?= $this->Form->end()?>

Table Field Is feature_image… but i am sending image through featureImage