Here is validation function in CakePHP 3.x for PostsTable
:
public function validationDefault(Validator $validator)
{
$validator
->add('id', 'valid', ['rule' => 'numeric'])
->allowEmpty('id', 'create');
$validator
->requirePresence('title', 'create')
->notEmpty('title','Please fill Title field');
$validator
->requirePresence('content', 'create')
->notEmpty('content','Please add a Content');
return $validator;
}
The form in add
view is:
<?= $this->Form->create($post) ?>
<fieldset>
<legend><?= __('Add Post') ?></legend>
<?php
echo $this->Form->input('title');
echo $this->Form->input('content');
echo $this->Form->input('category_id', ['options' => $categories]);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
Why is the default validation message always shown instead of the notEmpty
message?
问题已解决,我将validationDefault(Validator $validator)
重命名为validationPost(Validator $validator)
,我也在PostsController
内部进行了此修改,增加了功能:
$post = $this->Posts->patchEntity($post, $this->request->data,['validate' => 'Post']);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.