2013-12-12 18 views
1

我正在嘗試執行自定義驗證。除$ 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 
      } 
     } 
    } 

我怎樣才能使這 - $> addError工作?

+0

結賬。可能是這可以幫助你http://www.yiiframework.com/forum/index.php/topic/2845-adderror-problem/ –

+0

我只看到它之前,但可悲的是它不工作 –

回答

0

我已經解決了這個問題,我張貼的答案,因爲它可能會幫助別人也。 我需要刪除

else{ 

//not the code inside 

} 
這種情況下

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

: - 我只是刪除了否則其大括號{}離開其他

裏面的代碼

因爲它是即: -

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

        )); 
0

自定義驗證應該從模型驗證中觸發;

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 
     } 

}

+0

先生,因爲我曾經說過,當**我拋出異常,然後它的工作**。所以很明顯,我已經**已經定義了它的規則** –

+1

這是在您的示例代碼中的位置?您的checkQuestion驗證例程不包含在您的模型驗證中(它沒有Yii驗證規則要求的參數)。最好在模型中添加所有的驗證,而不是在控制器中嘗試。 – ChrisB

+0

另一方面,從安全角度來看,您在會話中持有email_id,理論上這可能會被黑客更改。你應該在你的cWebUser類中保存這個。 – ChrisB