Class app\models\LoginForm
Inheritance | app\models\LoginForm » yii\base\Model |
---|---|
Source Code | https://github.com/yiisoft/yii2-app-basic/blob/master/models/LoginForm.php |
LoginForm is the model behind the login form.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$password | app\models\LoginForm | ||
$rememberMe | app\models\LoginForm | ||
$user | app\models\User|null | app\models\LoginForm | |
$username | app\models\LoginForm |
Public Methods
Method | Description | Defined By |
---|---|---|
getUser() | Finds user by $username | app\models\LoginForm |
login() | Logs in a user using the provided username and password. | app\models\LoginForm |
rules() | app\models\LoginForm | |
validatePassword() | Validates the password. | app\models\LoginForm |
Property Details
Method Details
Finds user by $username
public app\models\User|null getUser ( ) |
public function getUser()
{
if ($this->_user === false) {
$this->_user = User::findByUsername($this->username);
}
return $this->_user;
}
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;
}
public array rules ( ) | ||
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.
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.');
}
}
}