2010-12-23 31 views
5

我不知道是否有可能使用Flash Messenger沒有重定向?例如。登錄失敗後,我想繼續顯示錶單,不需要重定向。可能使用FlashMessenger而不重定向?

public function loginAction() { 
    $form = new Application_Form_Login(); 

    ... 

    if ($this->getRequest()->isPost() && $form->isValid($this->getRequest()->getParams())) { 
    $authAdapter = new Application_Auth_Adapter($form->getValue('username'), $form->getValue('password')); 
    if ($this->auth->authenticate($authAdapter)->isValid()) { 
     ... 
    } else { 
     // login failed 
     $this->flashMessenger->addMessage('Login failed. You may have entered an invalid username and/or password. Try again'); 
    } 
    } 

    $this->view->form = $form; 
} 

回答

8

您可以檢索,而不使用getCurrentMessages重定向$這個 - > flashMessenger->閃光消息(); 例如:

$this->view->messages = array_merge(
    $this->_helper->flashMessenger->getMessages(), 
    $this->_helper->flashMessenger->getCurrentMessages() 
); 
$this->_helper->flashMessenger->clearCurrentMessages(); 
3

當然,你可以。但我通常將auth-failure消息附加到表單本身。事實上,即使表單驗證失敗,我也喜歡展示「請注意下面的錯誤」。所以,我分別對待這兩種情況:

public function loginAction() 
{ 
    $form = new Application_Form_Login(); 
    if ($this->getRequest()->isPost()){ 
     if ($form->isValid($this->getRequest()->getPost())){ 
      $username = $form->getValue('username'); 
      $userpass = $form->getValue('userpass'); 
      $adapter = new Application_Model_AuthAdapter($username, $userpass); 
      $result = $this->_auth->authenticate($adapter); 
      if ($result->isValid()){ 
       // Success. 
       // Redirect... 
      } else { 
       $form->setErrors(array('Invalid user/pass')); 
       $form->addDecorator('Errors', array('placement' => 'prepend')); 
      } 
     } else { 
      $form->setErrors(array('Please note the errors below')); 
      $form->addDecorator('Errors', array('placement' => 'prepend')); 
     } 
    } 
    $this->view->form = $form; 
} 
+0

我該怎麼辦?使用我的上述代碼,刷新後只會顯示Flash信使消息 – 2010-12-23 12:23:45

+4

這是我不喜歡FlashMessenger的一個方面;它只存在於控制器級別。對我而言,這個Flash信使業務應該是一個以視圖爲中心的過程。這就是爲什麼我主要使用稱爲[priorityMessenger]的視圖幫助器(http://emanaton.com/code/php/zendprioritymessenger)。爲了使現有的FlashMessenger有用,您可以查看[Robert Basic的FlashMessenger視圖助手](https://github.com/robertbasic/phpplaneta/blob/master/library/PPN/View/Helper/FlashMessenger.php)。在你的情況下,沒有刷新,我仍然認爲將錯誤附加到表單上更簡單。 – 2010-12-23 15:23:53