public function validationDefault(Validator $validator)
    {
        $validator
            ->add('subject', [
                'notBlank' => [
                    'rule' => 'notBlank',
                    'message' => __d('comment', 'You need to provide a comment subject.'),
                ],
                'length' => [
                    'rule' => ['minLength', 5],
                    'message' => 'Comment subject need to be at least 5 characters long',
                ]
            ])
            ->add('body', [
                'notBlank' => [
                    'rule' => 'notBlank',
                    'message' => __d('comment', 'Your comment message cannot be empty')
                ],
                'length' => [
                    'rule' => ['minLength', 5],
                    'message' => 'Comment message need to be at least 5 characters long',
                ]
            ])
            ->allowEmpty('user_id')
            ->allowEmpty('parent_id')
            ->add('parent_id', 'checkParentId', [
                'rule' => function ($value, $context) {
                    if (!empty($value)) {
                        // make sure it's a valid parent: exists and belongs to the
                        // the same bundle (table)
                        $conditions = [
                            'id' => $value,
                            'entity_id' => $context['data']['entity_id'],
                            'table_alias' => $context['data']['table_alias'],
                        ];

                        return TableRegistry::get('Comment.Comments')->find()
                            ->where($conditions)
                            ->count() > 0;
                    } else {
                        $context['data']['parent_id'] = null;
                    }

                    return true;
                },
                'message' => __d('comment', 'Invalid parent comment!.'),
                'provider' => 'table',
            ]);

        return $validator;
    }
public function validationDefault(Validator $validator)
    {
        $validator
            ->add('subject', [
                'notBlank' => [
                    'rule' => 'notBlank',
                    'message' => __d('comment', 'You need to provide a comment subject.'),
                ],
                'length' => [
                    'rule' => ['minLength', 5],
                    'message' => 'Comment subject need to be at least 5 characters long',
                ]
            ])
            ->add('body', [
                'notBlank' => [
                    'rule' => 'notBlank',
                    'message' => __d('comment', 'Your comment message cannot be empty')
                ],
                'length' => [
                    'rule' => ['minLength', 5],
                    'message' => 'Comment message need to be at least 5 characters long',
                ]
            ])
            ->allowEmpty('user_id')
            ->allowEmpty('parent_id')
            ->add('parent_id', 'checkParentId', [
                'rule' => function ($value, $context) {
                    if (!empty($value)) {
                        // make sure it's a valid parent: exists and belongs to the
                        // the same bundle (table)
                        $conditions = [
                            'id' => $value,
                            'entity_id' => $context['data']['entity_id'],
                            'table_alias' => $context['data']['table_alias'],
                        ];

                        return TableRegistry::get('Comment.Comments')->find()
                            ->where($conditions)
                            ->count() > 0;
                    } else {
                        $context['data']['parent_id'] = null;
                    }

                    return true;
                },
                'message' => __d('comment', 'Invalid parent comment!.'),
                'provider' => 'table',
            ]);

        return $validator;
    }
public function validationDefault ( Validator $ validator ) { $ validator -> add ( 'subject' , [ 'notBlank' => [ 'rule' => 'notBlank' , 'message' => __d ( 'comment' , 'You need to provide a comment subject.' ) , ] , 'length' => [ 'rule' => [ 'minLength' , 5 ] , 'message' => 'Comment subject need to be at least 5 characters long' , ] ] ) -> add ( 'body' , [ 'notBlank' => [ 'rule' => 'notBlank' , 'message' => __d ( 'comment' , 'Your comment message cannot be empty' ) ] , 'length' => [ 'rule' => [ 'minLength' , 5 ] , 'message' => 'Comment message need to be at least 5 characters long' , ] ] ) -> allowEmpty ( 'user_id' ) -> allowEmpty ( 'parent_id' ) -> add ( 'parent_id' , 'checkParentId' , [ 'rule' => function ( $ value , $ context ) { if ( ! empty ( $ value ) ) { // make sure it's a valid parent: exists and belongs to the // the same bundle (table) $ conditions = [ 'id' => $ value , 'entity_id' => $ context [ 'data' ] [ 'entity_id' ] , 'table_alias' => $ context [ 'data' ] [ 'table_alias' ] , ] ; return TableRegistry :: get ( 'Comment.Comments' ) -> find ( ) -> where ( $ conditions ) -> count ( ) > 0 ; } else { $ context [ 'data' ] [ 'parent_id' ] = null ; } return true ; } , 'message' => __d ( 'comment' , 'Invalid parent comment!.' ) , 'provider' => 'table' , ] ) ; return $ validator ; }
Basic validation set of rules.

@param \Cake\Validation\Validator $validator The validator object
@return \Cake\Validation\Validator
Basic validation set of rules .