Answer the question
In order to leave comments, you need to log in
Output authorization errors in yii2 modal window, how to do?
I have an authorization and registration form in a modal window (it doesn’t matter how it’s done, but for those who are interested through a widget)
It is necessary that when entering a login and password, it would display an error, incorrect login (under the login field) or incorrect password (under the password field) after clicking on the login button
In the standard LoginForm, only the password is validated, in order for the login to be validated, I created a method
public function validateUsername($attribute, $params)
{
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user) {
$this->addError($attribute, 'Не правильный логин.');
}
}
}
// username is validated
['username', 'validateUsername'],
public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
}
else {
$model->password = '';
return $this->render('login', [
'model' => $model,
]);
}
}
return $this->render('login', [
'model' => $model,
]);
Answer the question
In order to leave comments, you need to log in
You need client-side validation:
https://www.yiiframework.com/doc/guide/2.0/en/inpu...
, or ajax validation of the entire form before submitting:
https://github.com/yiisoft/yii2 /issues/1399#issuec...
As an option. After an unsuccessful login, you need to re-open the modal login form, but before rendering the form (widget), validation errors must be passed to the LoginForm() model.
For example:
$model->addError('username', 'Не верный логин пользователя');
return $this->render('login', [
'model' => $model,
]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question