开发者

Possible to use FlashMessenger without redirect?

I wonder if its possible to use flash messenger without redirect? eg. After a failed login, I want to continue to display the form, no redirect required.

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

  开发者_如何学JAVA...

  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;
}


You can retrieve flash messages without redirect using $this->flashMessenger->getCurrentMessages(); Example:

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


Sure, you can. But I usually attach the auth-failure message to the form itself. In fact, even when the form-level validation fails, I like to display something like "Please note the errors below". So, I treat those two cases separately:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜