YII2 Active Form show custom validation message

蓝咒 提交于 2021-01-07 06:31:30

问题


I have login form which, where I am implementing 2 Factor Authentication integrating twilio API.

The API is working fine and I can get the verification response for success or failure.

Now I am somehow not able to display the message "OTP verification Failed" on the form.

here is my controller code:

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

and in my view file I have tried like:

<?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(); ?>

what I am missing here?


回答1:


Remove the exit; after setting the "OTP verification failed!" message.

You are returning to nowhere just after you set that flash message.

Remember that for the flash message to work, your code must reach your view. If you return before, or return a JSON, it will never get to your client.



来源:https://stackoverflow.com/questions/65239734/yii2-active-form-show-custom-validation-message

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!