I got "Declaration of ~ must be compatible with~" error in CakePHP4🍓 Cookbook

Now I tring CMS Tutorial in CakePHP4🍓 Cookbook.
In this part " Update Validation Rules for Articles", I made code according to the text.

use Cake\Validation\Validator;

plus

public function validationDefault(Validator $validator)
{
    $validator
        ->allowEmptyString('title', false)
        ->minLength('title', 10)
        ->maxLength('title', 255)

        ->allowEmptyString('body', false)
        ->minLength('body', 10);

    return $validator;

I made following code.

<?php 
namespace App\Model\Table;

use Cake\ORM\Table;
//Textクラス
use Cake\Utility\Text;

use Cake\Validation\Validator;

class ArticlesTable extends Table
{
public function initialize(array $config) : void
{
    $this->addBehavior('Timestamp');
}

public function beforeSave($event, $entity, $options)
{
    if ($entity->isNew() && !$entity->slug) {
        $sluggedTitle = Text::slug($entity->title);
        //スラグをスキーマで定義されている最大長に調整
        $entity->slug = substr($sluggedTitle, 0, 191);
    }
}

public function validationDefault(Validator $validator)
{
    $validator
        ->allowEmptyString('title', false)
        ->minLength('title', 10)
        ->maxLength('title', 255)

        ->allowEmptyString('body', false)
        ->minLength('body', 10);

    return $validator;
}

}

Then reload articles page. But I encountered that error.

Fatal error: Declaration of App\Model\Table\ArticlesTable::validationDefault(Cake\Validation\Validator $validator) must be compatible with Cake\ORM\Table::validationDefault(Cake\Validation\Validator $validator): Cake\Validation\Validator in /home/+++++/*******.com/public_html/cms/src/Model/Table/ArticlesTable.php on line 10

I can’t help it.
Someone please tell me how I solve this?
Thank you.

The documentation you’ve referenced says that the function prototype is

public function validationDefault(Validator $validator): Validator

You’ve got only

public function validationDefault(Validator $validator)

Those are not exact matches.

1 Like

Thank you helping.
this problem is all of my merely misstaken.
when I wrote,

public function validationDefault(Validator $validator): Validator

It completely worked.

In ja version document says

public function validationDefault(Validator $validator)

I suppose It’s officially wrong.

Yep, seems like that didn’t get updated. All the docs are in github, so you could submit a PR to correct that for others who follow.