cakePHP验证错误未显示

时间:2014-08-02 20:13:14

标签: validation cakephp

所以基本上我在我的用户控制器中有一个查看操作,用户可以修改他的信息(用户名,名字,姓氏,电子邮件......),此表单发送到另一个更新执行保存操作的操作,问题是当我提交表单并且一个或多个字段不符合验证规则时,它不显示在每个字段下面但数据不保存并且

  

$这 - >用户 - > validationErrors

输出错误。

我的更新操作(在view.ctp上提交表单后访问)

view.ctp:

<?php

        echo $this->Form->create('User', array(
            'inputDefaults' => array(
                'div' => 'form-group',
                'wrapInput' => false,
                'class' => 'form-control'
            ),
            'class' => 'well',
            'url'=>array('controller'=>'users','action'=>'update'),
            'id'=>'info-form'
        ));
        ?>

        <fieldset>
            <legend>Personal Information</legend>

            <?php
            echo $this->Form->input('id', array('value' => $userinfo['User']['id']));
            echo $this->Form->input('User.username', array(
                'label' => 'Username',
                'value'=>$userinfo['User']['username']
            ));
            ?>
            <td><?php echo $this->Form->error('username'); ?></td>
            <?php
            echo $this->Form->input('User.email', array(
                'label' => 'E-mail',
                'value'=>$userinfo['User']['email']
            ));
            ?>
            <?php
            echo $this->Form->input('User.fname', array(
                'label' => 'First name',
                'value'=>$userinfo['User']['fname']
            ));
            ?>
            <?php
            echo $this->Form->input('User.lname', array(
                'label' => 'Last name',
                'value'=>$userinfo['User']['lname']
            ));
            ?>
        <?php
        echo $this->Form->submit('Update', array(
            'div' => 'form-group',
            'class' => 'btn btn-success'
        ));
        ?>
        </fieldset>
        <?php echo $this->Form->end(); ?>

更新功能:

function update() {
        $this->autoRender = false;
        $this->User->set($this->request->data);

        if ($this->request->is('post')) {
                if ($this->User->save($this->request->data)) {
                    $this->Session->setFlash(__('Information updated Successfuly.'), 'alert', array(
                        'plugin' => 'BoostCake',
                        'class' => 'alert-success'), 'success');
                    return $this->redirect('/users/view/' . $this->request->data['User']['id']);
                } else {
                    // $errors = $this->User->validationErrors; var_dump($errors);die;
                    $this->Session->setFlash(__('An error occured'), 'alert', array(
                        'plugin' => 'BoostCake',
                        'class' => 'alert-danger'), 'danger');
                    return $this->redirect('/users/view/' . $this->request->data['User']['id']);
                }

        } else {
            $this->Session->setFlash(__('Request was not of POST type.'), 'alert', array(
                'plugin' => 'BoostCake',
                'class' => 'alert-danger'), 'danger');
            return $this->redirect('/users/index/');
        }

1 个答案:

答案 0 :(得分:3)

这是因为您之后会重定向 - 这将使其失去验证警告。