我目前正在使用Yii2框架。 在登录页面中,当我登录失败时,它只刷新视图,但不显示错误。 这里是我当前视图:登录失败时没有错误信息Yii2
<?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>
在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,
]);
}
}
...
无需花哨的东西在这里,只是要提醒打字不对劲,用户名或密码的用户。
我的想法是渲染错误在同一个登录页面... –
在这种情况下,你需要知道是第一次尝试还是第二次尝试是由错误引起的。更改您的LoginForm模型添加您需要的字段并在控制器/操作中分配适当的值 – scaisEdge
我可以只使用Flash消息吗? –