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

  • 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条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统