CommentsTable.php
<?php namespace App\Model\Table; use Cake\ORM\Table; use Cake\Validation\Validator; class CommentsTable extends Table { public function initialize(array $config) { $this->addBehavior('Timestamp'); $this->belognsTo('Posts'); } public function validationDefault(Validator $validator) { $validator ->notEmpty('body') ->requirePresence('body') return $validator; } }
PostsTable.php
<?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'); $this->hasMany('Comments'); } 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; } }