<?= $this->Form->create($post); ?>
<?= $this->Form->input('title'); ?>
<?= $this->Form->input('body', ['rows'=> '3']); ?>
<?= $this->Form->button('Add'); ?>
<?= $this->Form->end(); ?>
なるほど、だんだんテンションが下がってきた。

DBへのinsertをcontrollerで書きます。
public function add()
{
$this->viewBuilder()->layout('my_layout');
$post = $this->Posts->newEntity();
if($this->request->is('post')){
$post = $this->Posts->patchEntity($post, $this->request->data);
$this->Posts->save($post);
return $this->redirect(['action'=>'index']);
}
$this->set('post', $post);
}
validatorはmodelに書きます。
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class PostsTable extends Table
{
public function initialize(array $config)
{
$this->addBehavior('Timestamp');
}
public function validationDefault(Validator $validator)
{
$validator
->notEmpty('title')
->requirePresence('title')
->notEmpty('body')
->requirePresence('body')
->add('body', [
'length' => [
'rule' => ['minLength', 10],
'message' => 'body length must be 10+'
]
]);
return $validator;
}
}
controllerでvalidationの設定
public function add()
{
$this->viewBuilder()->layout('my_layout');
$post = $this->Posts->newEntity();
if($this->request->is('post')){
$post = $this->Posts->patchEntity($post, $this->request->data);
if($this->Posts->save($post)){
return $this->redirect(['action'=>'index']);
}else {
// error
}
}
$this->set('post', $post);
}