Cakephp 5 file uploading validation mimeType does not work

Try this:

public function add() {
    $article = $this->Articles->newEmptyEntity();
    if ($this->request->is('post')) {
        $article = $this->Articles->patchEntity($article, $this->request->getData());
        $article->user_id = $this->request->getAttribute('identity')->getIdentifier();
        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'));
}

You change the value of the feature_image variable before the patchEntity, so it can’t validate.

And use this validator

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

that is here before save data… and even you can check featureImage file object

and here you can see in feature_image field… please look at the extension (webp)… file upload from client and it does not throw error but it must have throw error here because in validation i just allow only JPG JPEG PNG pictures… Me also little confused bro @shadowx.jb and i really thanks you still helping me

this way i am getting error… of course it throw error if i send direct file object in PatchEntity method… so that why i am just setting featureImage field in from and during the save data i just fill the value of feature_image with the image Url… that all handle by component

I try it and its works…

Entity

class Article extends Entity
{
    protected $_accessible = [
        'article_title' => true,
        'article_content' => true,
        'user_id' => true,
    ];
}

Table

public function validationDefault(Validator $validator): Validator
{
    $validator
        ->scalar('article_title')
        ->requirePresence('article_title', 'create')
        ->notEmptyString('article_title');

    $validator
        ->scalar('article_content')
        ->requirePresence('article_content', 'create')
        ->notEmptyString('article_content');

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

    return $validator;
}

Template

echo $this->Form->create($article, ['type' => 'file']);
echo $this->Form->control('user_id', [
    'value' => 1,
]);
echo $this->Form->control('article_title');
echo $this->Form->control('article_content');
echo $this->Form->control('featureImage', [
    'type' => 'file',
]);
echo $this->Form->end();

Controller

$article = $this->Article->newEmptyEntity();
if ($this->request->is('post')) {
    $article = $this->Article->patchEntity($article, $this->request->getData());
    if ($this->Article->save($article)) {
        $this->Flash->success('Success');

        return $this->redirect(['action' => 'index']);
    }
    $this->Flash->error('error');
}
$this->set(compact('article'));

Hi @shadowx.jb now i followed same steps as you mention above but now i am getting this :-

you seem to be trying to save a uploaded file into a string column in the database.

This means validation has passed and the Cake ORM tried to convert the submitted data into something the database can understand (in your example its the StringType because your column is probably a varchar or something like that)

1 Like

so now problem solved :rofl:. Actually there is some logical and common scenes error :laughing: but now all are solved my code is working fine…

thank to all guys @shadowx.jb @KevinPfeifer @Zuluru

1 Like

Hi… @shadowx.jb now my code work fine… i just change my controller code of flow then all problems solved

1 Like