2012-02-24 59 views
1

Yii的小白在這裏。登錄形式

我製成,其呈現爲局部下拉登錄表單。當用戶憑證正確無誤並重定向到任何地方時,它都能正常工作。但是,如果表單驗證或身份驗證失敗,我無法將錯誤消息傳遞給登錄表單。我認爲,問題是,我的登錄表單是主要佈局(因爲我希望它在任何時候訪問)和控制器不能在清潔的方式變量的佈局傳遞。

因此,這裏是在SiteController

public function actionLogin() 
     {      
       $form = new LoginForm; 
       if (isset($_POST['LoginForm'])) 
       { 
         $form->attributes=$_POST['LoginForm']; 

         //validate user input and redirect to user panel if valid 
         if($form->validate() && $form ->login()) 
         { 
           $success = "oui"; 
           $this->render('user_panel');  
         } 
         else 
         {            
           $success = $form->getError('login').$form->getError('password');               
           $this->render('index',array('success'=>$success)); 
           //here I can pass the error messages to the index view for example, but not to the login form in the main layout, how can I do this? 
         }                          
       }      
     } 

而且我的登錄操作,登錄表單中,我設置enableAjaxValidation爲真,這是如何工作的?我可以對控制器說,也可以通過Ajax直接回復表單嗎?

最後一個問題:形式應該回聲$形式 - > errorSummary($模型)&模型是新LoginForm的。爲什麼不顯示錯誤摘要?

感謝您的關注

回答

1

此:

$success = $form->getError('login').$form->getError('password');               
$this->render('index',array('success'=>$success)); 

應該是:

$this->render('login',array('model'=>$model)); 

的形式應該有幾條線象

<?php echo $form->error($model,'field'); ?> 

<?php echo $form->errorSummary($model); ?> 

這些行將從驗證程序接收到錯誤並顯示它們,因此在發生錯誤時不需要傳遞任何內容。

最後,爲了獲得最佳實踐,您的命名約定會導致分析錯誤,您的錯誤變量實際上稱爲$ success。

+0

就像我在開始時所說的登錄表單是在佈局中,所以傳遞錯誤似乎沒有這樣工作。實際上,不是呈現'索引',我應該渲染用戶登錄的任何視圖。 – Arthur 2012-02-27 09:08:43

+0

$模型應該是新的LoginForm的權利? – Arthur 2012-02-27 09:12:32