如何在登录表单模式中显示错误消息? [英] How can I show error message in login form modal?

查看:41
本文介绍了如何在登录表单模式中显示错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注这个 link 创建登录模式.现在我的登录操作是

I am following this link to create a login modal. Now my login action is

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->renderAjax('login', [
                'model' => $model,
        ]);         
    }
}

在另一个视图 test.php 中,我有一个按钮

and in another view test.php, I have a button

echo Html::button('Create New Company',
                  ['value' => Url::to(['site/login']),
                   'title' => 'Creating New Company', 
                   'class' => 'showModalButton btn btn-success'
                  ]
                ); 

单击此按钮时,它会将登录表单显示为模态并对所需属性进行客户端验证.当提供正确的用户名和密码时,用户也可以成功登录.

On clicking this button, it is showing the login form as modal and doing client side validation for required attributes. When correct username and password is provided, the user gets successfully logged in too.

我的问题是,当我们传递错误的用户名或密码凭据时,它会在另一个没有样式的页面中呈现 site/login 并显示错误 不正确的用户名或密码那里.如何在模态上显示该错误?

My problem is when we pass the wrong credential's for username or password, it is rendering site/login in another page which is unstyled and showing error Incorrect username or password there. How can I show that error on modal?

推荐答案

像下面这样使用 ajax 验证

Use ajax validation like below

形式上

'enableAjaxValidation' => 真,

'enableAjaxValidation' => true,

在控制器动作中使用下面的代码

In controller action use below code

if (Yii::$app->request->isAjax && $model->load($_POST))
{
  Yii::$app->response->format = 'json';
  return \yii\widgets\ActiveForm::validate($model);
}

这篇关于如何在登录表单模式中显示错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆