如何更改CakePHP 3.x验证错误消息

时间:2016-01-09 18:52:01

标签: php cakephp cakephp-3.x

以下是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消息?

1 个答案:

答案 0 :(得分:0)

问题已修复,我将validationDefault(Validator $validator)重命名为validationPost(Validator $validator),我还在PostsController内添加了修改功能:

$post = $this->Posts->patchEntity($post, $this->request->data,['validate' => 'Post']);