php - No error message when Login fail Yii2

645

I'm currently using Yii2 framework. In the Login page,when I have a failed login, it just refreshes the view, but no errors displayed. Here's my current view:

In SiteController:

...
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,
            ]);
        }
    }
...

No need of fancy stuff here, just something to alert users that are typing something wrong, user or password.

601

Answer

Solution:

if u want flash message then try this. in controller

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 {
            Yii::$app->session->setFlash('failure', "incorrect username or password");
            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

in view add this code along with your current code

<?php if (Yii::$app->session->hasFlash('failure')): ?>
      <div >
        <button aria-hidden="true" data-dismiss="alert" type="button">×</button>
        <h4><i ></i> Error!</h4>
        <?= Yii::$app->session->getFlash('failure') ?>
      </div>
      <?php endif; ?>
919

Answer

Solution:

public function actionLogin()
{
    if (!Yii::$app->user->isGuest) {
        return $this->goHome();
    }

    $model = new LoginForm();
    if ($model->load(Yii::$app->request->post())) {            
        if ($model->login()) {                
            return $this->goBack();
        } else {
            Yii::$app->session->setFlash('error', 'Invalid login details.');
        }            
        return $this->refresh();
    } else {
        return $this->render('login', [
            'model' => $model,
        ]);
    }
}
164

Answer

Solution:

This is exactly what your code does

    $model = new LoginForm(); // create e new model for login

    // check for the data provided: $model->load(Yii::$app->request->post())  == true 
    //  and 
    //  if the data are right: $model->login() == true 
    // then ->goBack()
    if ($model->load(Yii::$app->request->post()) && $model->login()) {
        return $this->goBack();

    // otherwise : no data provided or login incorrect 
    // return the render of the login page
    } else {
        return $this->render('login', [
            'model' => $model,
        ]);
    }

If you want a different behavior just provide proper code. eg: is no data provided render the login page but if the login data are provided but incorrect render a warning page and after render the login page

People are also looking for solutions to the problem: php - What is the best way to display menu in Laravel 5.1?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.