我正在使用bootstrap模式进行登录。这是我的控制器代码
public function actionSignin()
{
$this->layout= "main";
$model = new LoginForm();
if ( isset( $_POST[ 'LoginForm' ] ) )
{
if ( $model->load( Yii::$app->request->post() ) && $model->login() )
{
$session = Yii::$app->session;
$session['username'] = $model->user->first_name;
$session['userid'] = $model->user->id;
return $this->redirect( [ '/site/home' ] );
}
else
{
Yii::$app->getSession()->setFlash('error', 'Incorrect Email or Password or both');
return $this->renderAjax( 'signin', [ 'model' => $model ] );
}
}
else
{
return $this->renderAjax( 'signin', [ 'model' => $model ] );
}
}
当用户点击以下链接登录表单时弹出
echo Html::a( Yii::t( 'app', ' {modelClass}', ['modelClass' => 'SignIn',
] ), [ 'account/signin' ], [ 'class' => 'btn btn-link fa fa-sign-in btn-primary sign']
);
当用户输入正确的用户名和密码时,用户将被重定向到代码中给出的site / home。但是当用户输入错误的用户名或密码时,模式应该再次弹出,但事实并非如此。相反,我得到一个白色背景(没有布局)的页面与字段输入用户名和密码。我无法在模态弹出窗口中看到它为什么会这样?
答案 0 :(得分:0)
添加您的_form
,如:
<?php $form = ActiveForm::begin(['enableAjaxValidation' => true,]); ?>
在controller
文件中添加此代码:
public function actionSignin()
{
$this->layout= "main";
$model = new LoginForm();
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post()))
{
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
if ( $model->load( Yii::$app->request->post() ) && $model->login() )
{
$session = Yii::$app->session;
$session['username'] = $model->user->first_name;
$session['userid'] = $model->user->id;
return $this->redirect( [ '/site/home' ] );
}
else
{
Yii::$app->getSession()->setFlash('error', 'Incorrect Email or Password or both');
return $this->renderAjax( 'signin', [ 'model' => $model ] );
}
return $this->renderAjax( 'signin', [ 'model' => $model ] );
}