gpt4 book ai didi

php - Yii2:登录时设置不正确的用户名或密码消息

转载 作者:行者123 更新时间:2023-12-02 20:51:44 25 4
gpt4 key购买 nike

我在我的应用程序中使用 yii2 basic。当我通过输入错误的用户名或密码登录应用程序时,它不会设置任何消息,例如不正确的用户名或密码。浏览器只是刷新页面。

这是我的site/login.php文件:

<?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}\n<div class=\"col-lg-3\">{input}</div>\n<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>\n<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>

这是我的登录操作:

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

任何帮助将不胜感激。

最佳答案

您可以在规则中添加自定义消息错误。

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

它会创建 JavaScript 验证,如果验证错误,则会在消息中显示错误。

参见Validating Input自定义错误消息部分。

编辑:

如果您使用的是基本应用程序,请确保您的 LoginForm.php 具有登录表单调用 validate。像这样的事情:

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

这来自高级模板,但很相似。

关于php - Yii2:登录时设置不正确的用户名或密码消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32345994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com