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.