2010-12-23 32 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

相关问题