CakePHP 3验证输入消息

时间:2017-07-09 15:01:30

标签: validation cakephp cakephp-3.4

我在CakePHP 3中遇到验证问题。我预先编写了一些代码,并对email字段进行了验证,如下所示:

    $validator
        ->email('email')
        ->notEmpty('email')
        ->add('email', 'correct',[
            'rule' => [
                'custom', '/@example.com$/i'
            ],
            'message' => 'Not correct...'
        ]);

表单视图:

<?php
    echo $this->Form->create('Users', [
        'novalidate' => true
    ]);
?>
    <fieldset>
        <?php
            echo $this->Form->control('email', [
                'label' => __d('admin', 'E-mail')
            ]);
        ?>
    </fieldset>
    <?php echo $this->Form->button(__d('admin', 'Send')); ?>
<?php echo $this->Form->end(); ?>

和控制器内容:

    if ($this->request->is('post')) {
        $user = $this->Users->find()->select([
            'Users.' . $this->Users->getPrimaryKey()
        ])->where([
            'Users.email' => $this->request->getData('email')
        ])->first();

        if (!is_null($user)) {
            $this->Users->patchEntity($user, $this->request->getData());

            $user->uuid = 'random string...';

            if (empty($user->errors())) {
                if ($this->Users->save($user)) {
                    $this->Flash->success(__d('admin', 'Saved...'));
                } else {
                    $this->Flash->error(__d('admin', 'Not saved...'));
                }
            } else {
                $this->Flash->error(__d('admin', 'Form validation...'));

                pr($user->errors()); // Is not empty...
            }
        } else {
            $this->Flash->error(__d('admin', 'E-mail doesn't exists...'));
        }
    }

表单和控制器操作正常,但是没有在email字段下面显示验证消息,为什么?

0 个答案:

没有答案