douao2019
2013-12-12 05:13
浏览 260
已采纳

$ this-> addError在yii模型类中不起作用

I am trying to do the custom validation.All is working fine except $this->addError .
It is not adding the error as it should. this is my controller action

public function actionVerifyAnswer()
    {

        if(isset(Yii::app()->session['_emailId']))
        {
        $model=new LoginForm;
        $model->scenario='verifyAns';
        if(isset($_POST['LoginForm']))
        {

            $model->attributes=$_POST['LoginForm'];
            if($model->validate())
            {
            $theQuestion=$model->secretQuestion;
            $theAnswer=  strtolower($model->verifyAnswer);
            $usersModel=new Users;

            $emailUser=  Yii::app()->session['_emailId'];

            $userRecord=$usersModel->find('loginEmail=:email AND secretQuestion=:que',array(':email'=>$emailUser,':que'=>$theQuestion));
            if(empty($userRecord))
            {   
                throw new CHttpException('You have selected the wrong question');
            }
            else
            {
                $usersAnswer=  strtolower($userRecord->secretAnswer);
                if($theAnswer === $usersAnswer)
                {

                    $this->redirect('changePassword');
                }
                else
                {

                    throw new CHttpException('you have given the wrong answer');
                }
            }


        }
        else
        {
            throw new CHttpException('model->validate()');
        }
        }
        else
        {

             $secretQuestion= Yii::app()->params['secretQuestion'];
               $this->render('verifyAnswer', array('model'=>$model,
                                                    'secretQuestion'=>$secretQuestion,

                   ));
        }
        }
        else
        {

           Yii::app()->user->loginRequired();
        }

    }

and this is my custom validation in the model

public function checkQuestion()
    {
        if(!$this->hasErrors())
        {
            $model=new LoginForm;
            $theQuestion=$this->secretQuestion;
            $usersModel=new Users;
            $emailUser=  Yii::app()->session['_emailId'];
            $userRecord=$usersModel->find('loginEmail=:email AND secretQuestion=:que',array(':email'=>$emailUser,':que'=>$theQuestion));
            if(empty($userRecord))
            {   
                //this line is not working

                $this->addError('secretQuestion', 'Incorrect Combination');

                //if i throw exception here. Then exception is working
            }
        }
    }

How can i make $this->addError working?

2条回答 默认 最新

相关推荐 更多相似问题