Class common\models\LoginForm

Inheritancecommon\models\LoginForm » yii\base\Model
Source Code https://github.com/yiisoft/yii2-app-advanced/blob/master/common/models/LoginForm.php

Login form

Public Methods

Hide inherited methods

Method Description Defined By
login() Logs in a user using the provided username and password. common\models\LoginForm
rules() common\models\LoginForm
validatePassword() Validates the password. common\models\LoginForm

Protected Methods

Hide inherited methods

Method Description Defined By
getUser() Finds user by $username common\models\LoginForm

Property Details

Hide inherited properties

$password public property
public $password null
$rememberMe public property
public $rememberMe true
$username public property
public $username null

Method Details

Hide inherited methods

getUser() protected method

Finds user by $username

protected common\models\User|null getUser ( )

                protected function getUser()
{
    if ($this->_user === null) {
        $this->_user = User::findByUsername($this->username);
    }
    return $this->_user;
}

            
login() public method

Logs in a user using the provided username and password.

public boolean login ( )
return boolean

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

            
rules() public method

public void 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'],
    ];
}

            
validatePassword() public method

Validates the password.

This method serves as the inline validation for password.

public void validatePassword ( $attribute, $params )
$attribute string

The attribute currently being validated

$params array

The additional name-value pairs given in the rule

                public function validatePassword($attribute, $params)
{
    if (!$this->hasErrors()) {
        $user = $this->getUser();
        if (!$user || !$user->validatePassword($this->password)) {
            $this->addError($attribute, 'Incorrect username or password.');
        }
    }
}