问题描述
限时送ChatGPT账号..我目前正在使用 Yii2 框架.在登录页面中,当我登录失败时,它只会刷新视图,但不会显示任何错误.这是我目前的看法:
<?php使用 yii\helpers\Html;使用 yii\bootstrap\ActiveForm;/* @var $this yii\web\View *//* @var $form yii\bootstrap\ActiveForm *//* @var $model \common\models\LoginForm *///$this->title = '欢迎来到我的网站';//$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"]]);?><标题>登入</标题><字段集><部分><label class="label">User</label><label class="input"><i class="icon-append fa fa-user"></i><input type="text" name="登录表单[用户名]"><b class="tooltip tooltip-top-right"><i class="fa fa-user txt-color-teal"></i>Escribe tu Usuario</b></label></节><部分><label class="label">Contraseña</label><label class="input"><i class="icon-append fa fa-lock"></i><input type="password" name="登录表单[密码]"><b class="tooltip tooltip-top-right"><i class="fa fa-lock txt-color-teal"></i>输入您的密码</b>标签><div class="note"><a href="forgotpassword.html">忘记密码?</a></节><部分><label class="checkbox"><input id="rememberMe" type="checkbox" name="LoginForm[rememberMe]"><i></i>记住我标签></节></fieldset><页脚><button type="submit" class="btn btn-primary">恩特拉按钮></页脚><?php $form = ActiveForm::end();?>
<script type="text/javascript">$(函数(){$('#rememberMe').on('change', function( e ) {e.stopPropagation();this.value = this.checked ?1:0;});})
在站点控制器中:
<代码>...公共函数 actionLogin(){if (!\Yii::$app->user->isGuest) {返回 $this->goHome();}$model = new LoginForm();if ($model->load(Yii::$app->request->post()) && $model->login()) {返回 $this->goBack();} 别的 {返回 $this->render('登录', ['模型' =>$模型,]);}}...
这里不需要花哨的东西,只需提醒用户输入错误的用户名或密码即可.
解决方案public function actionLogin(){if (!Yii::$app->user->isGuest) {返回 $this->goHome();}$model = new LoginForm();if ($model->load(Yii::$app->request->post())) {如果($模型->登录()){返回 $this->goBack();} 别的 {Yii::$app->session->setFlash('error', 'Invalid login details.');}返回 $this->refresh();} 别的 {返回 $this->render('登录', ['模型' =>$模型,]);}}
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.
解决方案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,
]);
}
}
这篇关于登录失败 Yii2 时没有错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论