Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No error message when Login fail Yii2

Tags:

php

yii

yii2

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:

<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;

/* @var $this yii\web\View */   
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \common\models\LoginForm */

//$this->title = 'Welcome to my site';
//$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-login">
    <h1><?= Html::encode($this->title) ?></h1>
    <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-5 col-lg-4">
            <div class="well no-padding">
                <?php $form = ActiveForm::begin(["id"=>"login-form", "options"=>["class"=>"smart-form client-form"]]); ?>
                    <header>
                        Sign In
                    </header>

                    <fieldset>                                  
                        <section>
                            <label class="label">User</label>
                            <label class="input"> <i class="icon-append fa fa-user"></i>
                                <input type="text" name="LoginForm[username]">
                                <b class="tooltip tooltip-top-right"><i class="fa fa-user txt-color-teal"></i> Escribe tu Usuario</b></label>
                        </section>

                        <section>
                            <label class="label">Contraseña</label>
                            <label class="input"> <i class="icon-append fa fa-lock"></i>
                                <input type="password" name="LoginForm[password]">
                                <b class="tooltip tooltip-top-right"><i class="fa fa-lock txt-color-teal"></i> Type your Password</b> </label>
                            <div class="note">
                                <a href="forgotpassword.html">Forgot your password?</a>
                            </div>
                        </section>

                        <section>
                            <label class="checkbox">
                                <input id="rememberMe" type="checkbox" name="LoginForm[rememberMe]">
                                <i></i>Remember Me
                            </label>
                        </section>

                    </fieldset>
                    <footer>
                        <button type="submit" class="btn btn-primary">
                            Entrar
                        </button>
                    </footer>
                <?php $form = ActiveForm::end(); ?>

            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
 $(function() {
   $('#rememberMe').on('change', function( e ) {
     e.stopPropagation();
     this.value = this.checked ? 1 : 0;
   });
 })
</script>

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.

like image 950
Mr_LinDowsMac Avatar asked Oct 31 '22 17:10

Mr_LinDowsMac


2 Answers

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 class="alert alert-danger alert-dismissable">
                <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>
                <h4><i class="icon fa fa-times"></i> Error!</h4>
                <?= Yii::$app->session->getFlash('failure') ?>
            </div>
            <?php endif; ?>
like image 168
Bloodhound Avatar answered Nov 08 '22 08:11

Bloodhound


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,
        ]);
    }
}
like image 45
gvanto Avatar answered Nov 08 '22 07:11

gvanto