关于控件器的一个问题

这下面的代码在ViewModel里提交的时如果验证不通过应该输出验证错误的信息(XX不能为空这样的验证信息),可是实际上没有输出,也没有任何异常,只是刷新了一下页面。




public function actionIndex()

{

    $this->mySave();

    $model = new myModel();

    $resource = new CActiveDataProvider('myModel');

    $this->render('viewModel', array('dataProvider' => $resource , 'model' => $model;

}


private function mySave()

{

        $model = new myModel();

        if (isset($_POST['myModel'])) {

            $model->attributes = $_POST['myModel'];

            if ($model->save())

            {

                $this->redirect(array('viewModel' , 'id' => $model->Id));

            }

        }

}



这下面的两段都是可以的,对这个问题感到迷茫,所以特来问问




public function actionIndex()

{

    $model = new myModel();

    $this->mySave($model);

    $resource = new CActiveDataProvider('myModel');

    $this->render('viewModel', array('dataProvider' => $resource , 'model' => $model;

}


private function mySave($model=null)

{

        $model = $model==null?new myModel():$model;

        if (isset($_POST['myModel'])) {

            $model->attributes = $_POST['myModel'];

            if ($model->save())

            {

                $this->redirect(array('viewModel' , 'id' => $model->Id));

            }

        }

}






public function actionIndex()

{

    $model = new myModel();

    if (isset($_POST['myModel'])) {

            $model->attributes = $_POST['myModel'];

            if ($model->save())

            {

                $this->redirect(array('viewModel' , 'id' => $model->Id));

            }

    }

    $resource = new CActiveDataProvider('myModel');

    $this->render('viewModel', array('dataProvider' => $resource , 'model' => $model;

}



不知道上面的问题有没有遇到过的。我觉得原因是我的mySave方法里有一个new myModel,然后在actionIndex里也有一个new myModel,但为什么产生这个问题我不明白

谢谢

PHP5.3.1,yii-docs-1.1rc.r1585

你在mySave后又重新创建了新的model,并且没有从$_POST里取值作验证,所以就没有错误信息了。

oh,明白了,谢谢。原来错误信息是放在model里然后在view里用errorsummary输出的。需要把model传回来,在view输出。原来自已一直纠缠在以为是直接在model的save方法里输出,忽略了view里的errorsummary($model);