doutu1889
2015-09-02 06:26
浏览 478
已采纳

Yii2:登录时设置错误的用户名或密码信息

I am using yii2 basic for my application. When I login to my application by typing wrong username or password, it does not set any message like incorrect username or password. The browser simply refreshes the page.

This is my site/login.php file:

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

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

$this->title = 'Login';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-login">
    <h1><?= Html::encode($this->title) ?></h1>

    <p>Please fill out the following fields to login:</p>

    <?php $form = ActiveForm::begin([
        'id' => 'login-form',
        'options' => ['class' => 'form-horizontal'],
        'fieldConfig' => [
            'template' => "{label}
<div class=\"col-lg-3\">{input}</div>
<div class=\"col-lg-8\">{error}</div>",
            'labelOptions' => ['class' => 'col-lg-1 control-label'],
        ],
    ]); ?>

    <?= $form->field($model, 'username') ?>

    <?= $form->field($model, 'password')->passwordInput() ?>

    <?= $form->field($model, 'rememberMe')->checkbox([
        'template' => "<div class=\"col-lg-offset-1 col-lg-3\">{input} {label}</div>
<div class=\"col-lg-8\">{error}</div>",
    ]) ?>

    <div class="form-group">
        <div class="col-lg-offset-1 col-lg-11">
            <?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
        </div>
    </div>

    <?php ActiveForm::end(); ?>

   <!--  <div class="col-lg-offset-1" style="color:#999;">
       You may login with <strong>admin/admin</strong> or <strong>demo/demo</strong>.<br>
       To modify the username/password, please check out the code <code>app\models\User::$users</code>.
   </div> -->
</div>

And here is my action for login:

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

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post())) {

            $model->password = md5($model->password);
            $model->login();
            return $this->redirect(['site/index']);
        }
        return $this->render('login', [
            'model' => $model,
        ]);
    }

Any help would be greatly appreciated.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongpa6867 2015-09-02 09:01
    已采纳

    You can add a custom message error in your rules.

    public function rules()
    {
        return [
            ['username', 'required', 'message' => 'Please choose a username.'],
        ];
    }
    

    It creates a JavaScript vaidation and shows the error in message if validation was wrong.

    See Validating Input and Customizing Error Messages section.

    EDIT:

    If you are using the basic app, make sure that your LoginForm.php has the login form calls validate. Something like this:

    public function login()
    {
        if ($this->validate()) {
            return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
        } else {
            return false;
        }
    }
    

    This is from the advanced template but is similar.

    打赏 评论
  • doxrxwn2252 2015-09-02 06:31

    The if ($model->load())-statement does no validation at all. It simply determines if the form in question was POSTed and that the POST data was loaded into the form instance. I'm assuming that the login() function returns a bool so you'll have to check its return result as well:

    public function actionLogin()
    {
        if (!\Yii::$app->user->isGuest) {
            return $this->goHome();
        }
    
        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post())) {
    
            $model->password = md5($model->password);
            if ($model->login()) {
               return $this->redirect(['site/index']);
            } 
        }
        return $this->render('login', [
            'model' => $model,
        ]);
    }
    

    If the login function correctly uses the model::addError() function it should then also display errors.

    The login-function of the basic application does run the validation, as @Sageth pointed out, so the change I proposed will work as expected.

    打赏 评论

相关推荐 更多相似问题