2015-10-20 54 views
4

我目前正在使用Yii2框架。 在登錄頁面中,當我登錄失敗時,它只刷新視圖,但不顯示錯誤。 這裏是我當前視圖:登錄失敗時沒有錯誤信息Yii2

<?php 
use yii\helpers\Html; 
use yii\bootstrap\ActiveForm; 

/* @var $this yii\web\View */ 
/* @var $form yii\bootstrap\ActiveForm */ 
/* @var $model \common\models\LoginForm */ 

//$this->title = 'Welcome to my site'; 
//$this->params['breadcrumbs'][] = $this->title; 
?> 
<div class="site-login"> 
    <h1><?= Html::encode($this->title) ?></h1> 
    <div class="row"> 
     <div class="col-xs-12 col-sm-12 col-md-5 col-lg-4"> 
      <div class="well no-padding"> 
       <?php $form = ActiveForm::begin(["id"=>"login-form", "options"=>["class"=>"smart-form client-form"]]); ?> 
        <header> 
         Sign In 
        </header> 

        <fieldset>         
         <section> 
          <label class="label">User</label> 
          <label class="input"> <i class="icon-append fa fa-user"></i> 
           <input type="text" name="LoginForm[username]"> 
           <b class="tooltip tooltip-top-right"><i class="fa fa-user txt-color-teal"></i> Escribe tu Usuario</b></label> 
         </section> 

         <section> 
          <label class="label">Contraseña</label> 
          <label class="input"> <i class="icon-append fa fa-lock"></i> 
           <input type="password" name="LoginForm[password]"> 
           <b class="tooltip tooltip-top-right"><i class="fa fa-lock txt-color-teal"></i> Type your Password</b> </label> 
          <div class="note"> 
           <a href="forgotpassword.html">Forgot your password?</a> 
          </div> 
         </section> 

         <section> 
          <label class="checkbox"> 
           <input id="rememberMe" type="checkbox" name="LoginForm[rememberMe]"> 
           <i></i>Remember Me 
          </label> 
         </section> 

        </fieldset> 
        <footer> 
         <button type="submit" class="btn btn-primary"> 
          Entrar 
         </button> 
        </footer> 
       <?php $form = ActiveForm::end(); ?> 

      </div> 
     </div> 
    </div> 
</div> 

<script type="text/javascript"> 
$(function() { 
    $('#rememberMe').on('change', function(e) { 
    e.stopPropagation(); 
    this.value = this.checked ? 1 : 0; 
    }); 
}) 
</script> 

在SiteController:

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

無需花哨的東西在這裏,只是要提醒打字不對勁,用戶名或密碼的用戶。

回答

0

這是你的代碼不正是

$model = new LoginForm(); // create e new model for login 

    // check for the data provided: $model->load(Yii::$app->request->post()) == true 
    // and 
    // if the data are right: $model->login() == true 
    // then ->goBack() 
    if ($model->load(Yii::$app->request->post()) && $model->login()) { 
     return $this->goBack(); 

    // otherwise : no data provided or login incorrect 
    // return the render of the login page 
    } else { 
     return $this->render('login', [ 
      'model' => $model, 
     ]); 
    } 

如果你想有一個不同的行爲,只需要提供正確的代碼。例如:沒有提供數據呈現登錄頁面,但是如果提供登錄數據但不正確呈現警告頁面並在呈現登錄頁面後

+0

我的想法是渲染錯誤在同一個登錄頁面... –

+0

在這種情況下,你需要知道是第一次嘗試還是第二次嘗試是由錯誤引起的。更改您的LoginForm模型添加您需要的字段並在控制器/操作中分配適當的值 – scaisEdge

+0

我可以只使用Flash消息嗎? –

1

如果您想要flash消息,請嘗試此操作。在控制器

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 { 
      Yii::$app->session->setFlash('failure', "incorrect username or password"); 
      return $this->render('login', [ 
       'model' => $model, 
      ]); 
     } 
    } 

鑑於添加此代碼與您現有的代碼一起

<?php if (Yii::$app->session->hasFlash('failure')): ?> 
      <div class="alert alert-danger alert-dismissable"> 
       <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button> 
       <h4><i class="icon fa fa-times"></i> Error!</h4> 
       <?= Yii::$app->session->getFlash('failure') ?> 
      </div> 
      <?php endif; ?> 
1
public function actionLogin() 
{ 
    if (!Yii::$app->user->isGuest) { 
     return $this->goHome(); 
    } 

    $model = new LoginForm(); 
    if ($model->load(Yii::$app->request->post())) {    
     if ($model->login()) {     
      return $this->goBack(); 
     } else { 
      Yii::$app->session->setFlash('error', 'Invalid login details.'); 
     }    
     return $this->refresh(); 
    } else { 
     return $this->render('login', [ 
      'model' => $model, 
     ]); 
    } 
} 
+0

還沒有測試過這個代碼,但是這個答案並沒有把'$ model-> load(Yii :: $ app-> request-> post())'和'$ model-> login()所以你可以檢測失敗的登錄嘗試。 – johnsnails

相關問題