Php 在Yii框架中对null上的成员函数validatePassword()调用时出错

Php 在Yii框架中对null上的成员函数validatePassword()调用时出错,php,yii,phpmyadmin,yii2,yii2-advanced-app,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

我是这个框架的新手,实际上这是我第一次使用这个框架 有人能帮我解决这个错误吗?我不知道如何修复这个错误?这是我的密码:

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


    }
问题-在null上调用成员函数validatePassword()
我在这里遇到的实际问题
公共函数validatePassword($attribute,$params)
{
//在loginForm.php文件模型中
loginForm.php模型
名称空间应用程序\模型;
使用Yii;
使用yii\base\Model;
/**
*LoginForm是登录表单背后的模型。
*
*@property User | null$User此属性为只读。
*
*/
类LoginForm扩展了模型
{
公共$username;
公共密码;
public$rememberMe=true;
private$\u user=false;
/**
*@return数组验证规则。
*/
公共职能规则()
{
返回[
//用户名和密码都是必需的
[['username','password'],'required'],
//rememberMe必须是布尔值
['rememberMe','boolean'],
//密码由validatePassword()验证
['password','validatePassword'],
];
}
/**
*验证密码。
*此方法用作密码的内联验证。
*
*@param string$attribute当前正在验证的属性
*@param array$params规则中给出的其他名称-值对
*/
公共函数validatePassword($attribute,$params)
{
如果(!$this->hasErrors()){
$user=$this->getUser();
回声“;
    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]);
        }

    }
如果(!$user |!$user->validatePassword($this->password)){ $this->addError($attribute,“用户名或密码不正确”); } } } /** *使用提供的用户名和密码登录用户。 *@return bool用户是否成功登录 */ 公共函数登录() { 如果($this->validate()){ 返回Yii::$app->user->login($this->getUser(),$this->rememberMe?3600*24*30:0); } 返回false; } /** *通过[[用户名]]查找用户 * *@return User | null */ 公共函数getUser() { 如果($this->\u user===false){ $this->_user=user::findByUsername($this->username); } 返回$this->\u用户; } }
我无法找出错误的确切原因

这是user.php模型文件

Yi2有一个优秀的调试器,但是您必须在开发环境中才能使用它,所以从命令行、cd到项目的根运行
php init dev

无论是在控制器中还是例如在loginForm.php中

Yii::info('password is:'。$this->passwrod);
在validatePassword()函数中,以确保获得预期的输出

现在尝试登录并查看日志以了解您的关键信息:

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


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

您的密码为空。因此,如果出现错误,请通过printinyes password value get var_dump($this->password)@mdsaif u tring调用
validatePassword()检查您传递的密码是否有任何值
为空。例如,您有变量$user,您希望它包含
user
model,但它为空。因此,您应该找到一个获取用户模型的位置,并检查它是否为空。