2017-05-29 73 views
1

我是這個框架中的新實際這是我第一次使用框架 有人可以幫助我解決這個錯誤我不知道如何解決這個錯誤?這裏是我的代碼:錯誤 - 調用Yii框架中的null成員函數validatePassword()

The problem- Call to a member function validatePassword() on null 

the actual problem I am getting here 
public function validatePassword($attribute, $params) 
    { 
//in loginForm.php file model 

loginForm.php model 
    namespace app\models; 

    use Yii; 
    use yii\base\Model; 

    /** 
    * LoginForm is the model behind the login form. 
    * 
    * @property User|null $user This property is read-only. 
    * 
    */ 
    class LoginForm extends Model 
    { 
     public $username; 
     public $password; 
     public $rememberMe = true; 

     private $_user = false; 


     /** 
     * @return array the validation rules. 
     */ 
     public function rules() 
     { 
      return [ 
       // username and password are both required 
       [['username', 'password'], 'required'], 
       // rememberMe must be a boolean value 
       ['rememberMe', 'boolean'], 
       // password is validated by validatePassword() 
       ['password', 'validatePassword'], 
      ]; 
     } 

     /** 
     * Validates the password. 
     * This method serves as the inline validation for password. 
     * 
     * @param string $attribute the attribute currently being validated 
     * @param array $params the additional name-value pairs given in the rule 
     */ 
     public function validatePassword($attribute, $params) 
     { 
      if (!$this->hasErrors()) { 
       $user = $this->getUser(); 

       echo"<pre>"; 


       if (!$user || !$user->validatePassword($this->password)) { 
        $this->addError($attribute, 'Incorrect username or password.'); 
       } 
      } 
     } 

     /** 
     * Logs in a user using the provided username and password. 
     * @return bool whether the user is logged in successfully 
     */ 
     public function login() 
     { 
      if ($this->validate()) { 
       return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0); 
      } 
      return false; 
     } 

     /** 
     * Finds user by [[username]] 
     * 
     * @return User|null 
     */ 
     public function getUser() 
     { 
      if ($this->_user === false) { 
       $this->_user = User::findByUsername($this->username); 
      } 

      return $this->_user; 
     } 


    } 

我無法找出錯誤的確切原因。

this is user.php model file 

    <?php 

    namespace app\models; 

    class User extends \yii\base\Object implements \yii\web\IdentityInterface 
    { 
     public $id; 
     public $username; 
     public $password; 
     public $admin_email; 
     public $authKey; 
     public $accessToken; 

    public static function tableName() 
     { 
      return 'admin_info'; 
     } 

     /** 
     * @inheritdoc 
     */ 
     public function rules() 
     { 
      return [ 
       #[['username', 'admin_email', 'password'], 'required'], 
       [['username','password'],'required'], 
       [['username', 'password'], 'string', 'max' => 20], 
       [['admin_email'], 'string', 'max' => 50], 
       ['admin_email','email'], 
       ['password','password'], 

      ]; 
     } 

     /** 
     * @inheritdoc 
     */ 
     public function attributeLabels() 
     { 
      return [ 
       'admin_id' => 'Admin ID', 
       'username' => 'Username', 
       'admin_email' => 'Admin Email', 
       'password' => 'Password', 

      ]; 
     } 


     /** 
     * @inheritdoc 
     */ 
     public static function findIdentity($id) 
     { 
      return isset(self::$users[$id]) ? new static(self::$users[$id]) : null; 
     } 

     /** 
     * @inheritdoc 
     */ 
     public static function findIdentityByAccessToken($token, $type = null) 
     { 
      foreach (self::$users as $user) { 
       if ($user['accessToken'] === $token) { 
        return new static($user); 
       } 
      } 

      return null; 
     } 

     /** 
     * Finds user by username 
     * 
     * @param string $username 
     * @return static|null 
     */ 
     public static function findByUsername($username) 
     { 
      foreach (self::$users as $user) { 
       if (strcasecmp($user['username'], $username) === 0) { 
        return new static($user); 
       } 
      } 

      return null; 
     } 

     /** 
     * @inheritdoc 
     */ 
     public function getId() 
     { 
      return $this->id; 
     } 

     /** 
     * @inheritdoc 
     */ 
     public function getAuthKey() 
     { 
      return $this->authKey; 
     } 

     /** 
     * @inheritdoc 
     */ 
     public function validateAuthKey($authKey) 
     { 
      return $this->authKey === $authKey; 
     } 

     /** 
     * Validates password 
     * 
     * @param string $password password to validate 
     * @return bool if password provided is valid for current user 
     */ 
     public function validatePassword($password) 
     { 
      return $this->password === $password; 
     } 

      public static function findByUsername($username){ 

      return static::findOne(['username' => $username]); 
     } 

    } 
+0

你的密碼是空的,因此錯誤,檢查你傳遞的密碼是否有任何值或者沒有通過printin – Exprator

+0

yes密碼值越來越var_dump($ this-> password) –

+0

@mdsaif要調用'validatePassword ()'爲null。例如,你有變量$ user,你期望它包含'User'模型,但它是空的。所以你應該找一個你得到用戶模型的地方,並檢查它是否爲空。 – SiZE

回答

1

Yii2具有優良的調試器,但你必須在開發環境從一個命令行中使用它的話,那麼cd到項目運行的php init-dev的根源。

無論是在您的控制器或例如在loginForm.php暫時放置在validatePassword()函數

Yii::info('password is:' . $this->passwrod);,以確保您得到預期的輸出。

現在嘗試登錄並查看日誌中的關鍵信息:

enter image description here

如果什麼都沒有,也許添加更多的信息,在以前的接觸點登錄。

記住不要在生產環境中使用調試並刪除您的調試代碼。

相關問題