手动CakePHP验证问题

时间:2015-08-20 15:04:53

标签: php cakephp

我尝试通过beforeValidate手动验证字段:

Post模型中:

function beforeValidate($options = array()) {
    $p = Router::getParams();
    //if it is step 3
    if ($p['pass'][0] == '3') {
        //if the user hasn't chosen any subject
        if (empty($this->data['Student'][0]['Subject'])) {

            $this->validationErrors['Student'][0]['Subject'] = 'Your error message';
            return false;
        }
        return true;
    }
}

我使用debugKit检查它是否传递给PostController

array(
    'Student' => array(
        (int) 0 => array(
            'Subject' => 'Your error message'
        )
    )
)

但是当我试图在视图中回应它时没有任何显示..

<?php echo $this->Form->error('Student.0.Subject'); ?>

编辑:

最后,我通过将控制器传递给视图来手动回显错误消息:

$this->set('error', $this->Post->validationErrors);

但是$this->Post->validationErrors会覆盖存储在同一个密钥中的所有其他数据..

0 个答案:

没有答案