When a user enters an incorrect username/password combination and clicks Submit button, I want to return an error message and display it to the user above the form without a page refresh.
I’m assuming this is the function that handles the generation of the error message, but I don’t know how to return it to be used in the UI.
common\models\LoginForm.php:
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) {
$this->addError($attribute, 'Incorrect username or password.');
}
}
}
The following code works to authenticate the user just fine and as you can see I’ve disable the goHome() function in order to authenticate without page refresh…
backend\controllers\SiteController.php:
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 {
return $this->render('login', [
'model' => $model,
]);
}
}
Thanks – so I’ve modified my actionLogin which returns the errors that I want in JSON format but I don’t know how to make the $errors variable available to be used by JQuery – do you know how I can transfer this variable to Javascript so I can add to UI?
Here is my action code in case anyone else is wondering how to do this –
backend\controllers\SiteController.php:
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();
//echo 1;
} else {
// populate model attributes with user inputs
$model->attributes = \Yii::$app->request->post('LoginForm');
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
if ($model->validate()) {
// all inputs are valid
} else {
// validation failed: $errors is an array containing error messages
$errors = $model->errors;
//return $errors;
echo $errors;
}
//return $this->render('login', [
// 'model' => $model,
//]);
}
}
OK I figured it out, here is what I did in case anyone is interested –
The idea is to use Javascript to disable the default behavoir of the form and to use JQuery to send a post request using your form action (site/login) and make sure to set the Response to JSON and return it.
Then you can access the returned JSON object like "result.property"…
backend\controllers\SiteController.php:
public function actionLogin()
{
if (!\Yii::$app->user->isGuest) {
//return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post())) {
//return $this->goBack();
//echo 1;
// populate model attributes with user inputs
if ($model->validate() && $model->login()) {
// all inputs are valid
// echo successful;
} else {
$model->attributes = \Yii::$app->request->post('LoginForm');
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
// validation failed: $errors is an array containing error messages
$errors = $model->errors;
//return $errors;
return $errors;
}
} else {
return $this->render('login', [
'model' => $model,
]);
}
}