2013-01-07 56 views
0

我想在我的登錄添加新的自定義錯誤,如果user_status是「0」,那麼它應該給的錯誤,我出發如下:加入的錯誤代碼

UserIdentity.php

const ERROR_USERNAME_INACTIVE=68; 
    public function authenticate() 
    { 
     $user = Users::model()->findByAttributes(array('email'=>$this->username)); 


     if ($user===null) 
     { 
      $this->errorCode=self::ERROR_USERNAME_INVALID; 
     } 
     else if ($user->password !== md5($this->password)) 
     { 
      $this->errorCode=self::ERROR_PASSWORD_INVALID; 
     } 
     else if($user->user_status !='1') 
     { 
      $this->errorCode=self::ERROR_USERNAME_INACTIVE; 
     } 
     else 
     { 
      $this->errorCode=self::ERROR_NONE; 
      $this->_id = $user->id; 
     } 
     return !$this->errorCode; 
    } 

在login.php中

case UserIdentity::ERROR_NONE: 
        Yii::app()->user->login($identity); 
        break; 
       case UserIdentity::ERROR_USERNAME_INVALID: 
        $this->addError('username','No Such user is associated with us .'); 
        break; 
       case UserIdentity::ERROR_USERNAME_INACTIVE: 
        $this->addError('user_status','Sorry, this user is not activated yet'); 
       default: // UserIdentity::ERROR_PASSWORD_INVALID 

        $this->addError('password','Password is incorrect.'); 
        break; 

現在我得到的問題,當user_Status是!= 1,那麼它是正確給錯誤,但也給人,密碼不正確,而密碼是正確

回答

2

您忘了在最後一個case語句中添加break。不中斷將執行default

... 
    break; 
case UserIdentity::ERROR_USERNAME_INACTIVE: 
    $this->addError('user_status','Sorry, this user is not activated yet'); 
    break; 
default: // UserIdentity::ERROR_PASSWORD_INVALID 
... 
+0

thanx @topher .. –

0

對不起,我犯了一個愚蠢的錯誤,在切換大小寫之後,我忘記放入break語句,以便下一個默認語句也執行。

case UserIdentity::ERROR_USERNAME_INACTIVE: 
      $this->addError('user_status','Sorry, this user is not activated yet'); 
      **break;** 
       default: // UserIdentity::ERROR_PASSWORD_INVALID 

        $this->addError('password','Password is incorrect.'); 
        break;