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.