以下是我在index.php
视图中实施模态的方法:
<?= Html::button(
'Change Password',
['value' => Url::to(['changepassword']). '&id=' . $session['user_id'],
'class' => 'btn btn-success',
'id' => 'modButton'
]) ?>
<?= Yii::$app->session->getFlash('message'); ?>
<?php
Modal::begin(['id' => 'modal2']);
echo "<div id='modContent'></div>";
Modal::end();
?>
这是我的模态形式:
<?php $form = ActiveForm::begin(); ?>
<?= Yii::$app->session->getFlash('message'); ?>
</br>
<?= $form->field($model, 'password')->passwordInput(['value' => '', 'style' => 'width: 300px;'])->label('New Password') ?>
<?= $form->field($model, 'ConfirmNewPassword')->passwordInput(['value' => '', 'style' => 'width: 300px;']) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Change Password', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
在我的控制器中(以防万一你需要它):
if ($model->load(Yii::$app->request->post())) {
$model->password = sha1($model->password);
if($model->password !== sha1($model->ConfirmNewPassword)){
Yii::$app->session->setFlash('message', "Passwords don't match!");
return $this->redirect(['index']);
}
}
每当我在模态中输入无效密码时,页面会在单独的页面中重定向到模态表单,并显示错误消息。我希望当用户输入无效密码时,模态会保留在其中某处的错误消息中。
我该怎么做?
答案 0 :(得分:1)
是的,因为您重定向到&#34;索引&#34;如果密码在Controller中不相同,则执行操作:
if($model->password !== sha1($model->ConfirmNewPassword)){
Yii::$app->session->setFlash('message', "Passwords don't match!");
return $this->redirect(['index']);
}
应该成为:
if($model->password !== sha1($model->ConfirmNewPassword)){
Yii::$app->session->setFlash('message', "Passwords don't match!");
}
else {
return $this->redirect(['index']);
}