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

$ 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条回答 默认 最新

  • drob50257447 2013-12-12 11:00
    关注

    I have solved the issue and i am posting the answer because it might help somebody else also. I needed to remove the

    else{
    
    //not the code inside
    
    }
    

    of this condition

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

    Note:- I just removed the else and its braces {} and left the code inside the else

    as it was i.e :-

    $secretQuestion= Yii::app()->params['secretQuestion'];
                   $this->render('verifyAnswer', array('model'=>$model,
                                                        'secretQuestion'=>$secretQuestion,
    
                       ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动