Sharon
Sharon

Reputation: 3919

Declaration of validationDefault in Cakephp 4.0

I'm trying to create a site with CakePHP 4.0, but can't get the validation rules to work. I read the documentation, but wasn't sure where I was supposed to put the validationDefault function. I've put it in UsersTable.php, which looks like this:

<?php

namespace App\Model\Table;

use App\Model\Entity\User;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Rule\IsUnique;
use Cake\ORM\Table;
use Cake\Validation\Validator;

class UsersTable extends Table {

    public function initialize(array $config): void {

        $this->addBehavior('Timestamp');

        $this->hasMany('Requests')
                ->setDependent(true);

        $this->hasMany('Offers')
                ->setDependent(true);

    }

    public function validationDefault(Validator $validator) {

        $validator->notEmptyString('email', 'Please enter your email address')
                  ->notEmptyString('name', 'Please enter a name');

        return $validator;
    }
}

?>

So I'm expecting that if a user tries to sign in without an email or name, it should throw an error.

The form I have for registration is:

        echo $this->Form->create();
        echo $this->Form->control('name');
        echo $this->Form->control('email');
        echo $this->Form->control('password', array('type' => 'password'));
        echo $this->Form->control('confirm_password', array('type' => 'password'));
        echo $this->Form->button('Register');
        echo $this->Form->end();

and that sends the data to UsersController.php, which contains:

public function login() {

                // Otherwise, we're registering
                $users = TableRegistry::getTableLocator()->get('Users');
                $user = $users->newEntity($this->request->getData());
                if ($users->save($user)) {
                    $result = $this->Authentication->getResult();
                    if ($result->isValid()) {
                        $target = '/users/home';
                        return $this->redirect($target);
                    }
                }else {
                    $this->Flash->error('Please fix errors below');
                }
                exit;
            }

        }

    }

When I try to register without entering a name and email, I get the following error message:

Fatal error: Declaration of App\Model\Table\UsersTable::validationDefault(Cake\Validation\Validator $validator) must be compatible with Cake\ORM\Table::validationDefault(Cake\Validation\Validator $validator): Cake\Validation\Validator in /Applications/MAMP/htdocs/src/Model/Table/UsersTable.php on line 26

I can't see any obvious problems with my code. Where am I going wrong?

Upvotes: 1

Views: 1259

Answers (1)

Greg Schmidt
Greg Schmidt

Reputation: 5099

The declaration of the validationDefault function in Cake is:

public function validationDefault(Validator $validator): Validator

Your declaration is just

public function validationDefault(Validator $validator)

You need to add the return type declaration at the end in order to match. The docs don't seem to mention that, you might want to raise an issue on that.

Upvotes: 6

Related Questions