以下是CakePHP 3.x中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;
}
add
视图中的表单是:
<?= $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() ?>
为什么始终显示默认验证消息而不是notEmpty
消息?
答案 0 :(得分:0)
问题已修复,我将validationDefault(Validator $validator)
重命名为validationPost(Validator $validator)
,我还在PostsController
内添加了修改功能:
$post = $this->Posts->patchEntity($post, $this->request->data,['validate' => 'Post']);