错误 - 在Yii框架中调用null上的成员函数validatePassword()

时间:2017-05-29 09:07:29

标签: php yii phpmyadmin yii2 yii2-advanced-app

我是这个框架中的新手,实际上这是我第一次使用框架 有人可以帮助我解决这个错误我不知道如何解决这个错误?这是我的代码:

  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]);
        }

    }

1 个答案:

答案 0 :(得分:1)

Yii2有一个出色的调试器,但你必须在开发环境中才能从命令行使用它,所以cd到项目的根运行php init-dev

无论是在您的控制器中,还是在loginForm.php中暂时放置

validatePassword()函数中的

Yii::info('password is:' . $this->passwrod);可确保您获得预期的输出。

现在尝试登录并查看日志以获取关键信息:

enter image description here

如果没有,可以在以前的接触点添加更多信息记录。

请记住,不要在生产环境中使用debug并删除调试代码。