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


$this->addError is not working in yii model class

我正在尝试进行自定义验证。除$this->addError外,所有操作均正常
它没有按应有的方式添加错误。这是我的控制器动作

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();
        }
    }

这是我在模型中的自定义验证

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
            }
        }
    }

如何使$this->addError工作

自定义验证应在模型验证中触发;

 public function rules()
 {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('the_answer', 'checkQuestion'), 
                    ....
    );
}
public function checkQuestion($attribute,$params)
{
      ....
        if(empty($userRecord))
        {   
            //this line is not working
            $this->addError('secretQuestion', 'Incorrect Combination');
            //if i throw exception here. Then exception is working
        }

}

我已经解决了这个问题,我发布了答案,因为它可能也会帮助其他人。我需要删除

else{
//not the code inside
}

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

注意:-我刚刚删除了else及其大括号{}将代码留在else 中

,即:-

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