【问题标题】:YII2 Active Form show custom validation messageYII2 Active Form 显示自定义验证消息
【发布时间】:2020-12-10 17:51:45
【问题描述】:

我有登录表单,其中我正在实施集成 twilio API 的 2 因素身份验证。

API 工作正常,我可以获得成功或失败的验证响应。

现在我无法在表单上显示“OTP 验证失败”消息。

这是我的控制器代码:

公共函数 actionLogin() {

    $this->layout = '@app/themes/backend/login';
    $model = new LoginForm(['scenario' => 'login']);
        
        if (Yii::$app->request->isAjax && $model->load($_POST)) {
            $authy_id = Yii::$app->session->get('authy_id');
          //  var_dump($authy_id);exit;
          Yii::$app->response->format = 'json';
        //  var_dump($model);exit;
            if(!empty($authy_id)){                
                $authy_api = new AuthyApi('api-key');
                $token_entered_by_user = $model->otp;
               
                $verification = $authy_api->verifyToken($authy_id, $token_entered_by_user);
                

            if ($verification->ok()) {
                // correct token
              //  Yii::$app->response->format = 'json';
                return \yii\bootstrap\ActiveForm::validate($model);
            }else{
               // Yii::$app->response->format = 'json';
                $model->addError('OTP Verification Failed');
              print_r('error');
              Yii::$app->session->setFlash('error', "OTP verification failed!");
              
                exit;

            }

            }else{
            Yii::$app->response->format = 'json';
        return \yii\bootstrap\ActiveForm::validate($model);
            }
    }
    if ($model->load(Yii::$app->request->post()) && $model->login()) {                    
                if (Yii::$app->user->identity->user_role == 'admin') {
    
                    $path = "../" . Yii::$app->user->identity->user_role;    
                    return $this->redirect($path);
                } 
                elseif(Yii::$app->user->identity->user_role == 'customer') {
                    $path = "../" . Yii::$app->user->identity->user_role .'/default/index';
                    return $this->redirect($path);
                }
                
                else{
                    $path = "../" . "site/index";
                     return $this->redirect($path);
        ...
        }
        
        
    }
    return $this->render('login', [
        'model' => $model,
    ]);
}

在我的视图文件中我尝试过:

<?php pjax::begin(['id'=>'otp-error']); ?>
      <?php  if(Yii::$app->session->hasFlash('error')):?>
        <div class="info" id="otp-error">
       
        <?= Yii::$app->session->getFlash('error') ?>
        
      </div>
      <?php endif?>
      <?php pjax::end(); ?>

我在这里缺少什么?

【问题讨论】:

    标签: php ajax yii2


    【解决方案1】:

    设置“OTP验证失败!”后去掉exit;消息。

    在你设置了那条消息之后,你就无处可去。

    请记住,要使 Flash 消息起作用,您的代码必须到达您的视图。如果您之前返回,或者返回 JSON,它将永远不会到达您的客户端。

    【讨论】:

      猜你喜欢
      • 2018-06-15
      • 2021-10-20
      • 2016-03-03
      • 1970-01-01
      • 2019-02-22
      • 2014-08-08
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多