模型验证不起作用

时间:2013-04-16 13:50:17

标签: php validation model yii

我有以下行动处理登记册

 $model=new User('checkout');
        if(!empty($_POST['User'])){

            $model->attributes = $_POST['User'];
            //echo '<pre>';echo $model->username;die;
            $model->validate();
           echo  $model->getErrors(); die;
        }
        $this->render('register',array('model'=>$model));

和模型规则是

array('username', 'required', 'on' => 'checkout','message' => Yii::t('validation', 'Email can not be blank.')),
            array('username', 'unique', 'on' => 'checkout', 'message' => Yii::t('validation', 'Email has already been taken.')),

            array('username', 'unique'),
            array('passwordConfirm', 'required', 'on' => 'checkout','message' => Yii::t('validation', 'Confirm password can not be blank')),
            array('username', 'email', 'message' => Yii::t('validation', 'Invalid username/email format ')),
            //comapre passwords

表格发布数据为

Array
(
    [fname] => hemc
    [lname] => k
    [username] => demo
    [dob] => 
    [password] => 
    [passwordConfirm] => 
    [agree] => 0
)

问题 - :验证模型时没有错误。

1 个答案:

答案 0 :(得分:2)

getErrors会返回一个数组,你应该试试这个:

var_dump($model->getErrors());

print_r($model->getErrors());

PS:您应该使用errorSummary

CActiveForm中显示错误