views:

885

answers:

3

I have two questions related to the FlashMessenger view helper. Both questions are to do this code:

My action method:

private $_messages; // set to $this->_helper->FlashMessenger; in init()

public function loginAction() {
    // > login validation <

    // Switch based on the result code
    switch ($result->getCode()) {
        // > snip several cases <

        case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID:
            $this->_messages->addMessage("That wasn't the right password."); 
        break;

        case Zend_Auth_Result::SUCCESS:
            $this->_messages->addMessage('Logged you in successfully. Welcome back!');
            $this->_helper->Redirector('index', 'home');
        break;
    }

    // >snip<

    $this->view->messages = $this->_messages->getMessages();
    $this->render();
}

My layout (Zend_Layout) view script:

<?php if (isset($this->messages) && count($this->messages) > 0) {
    print_r($this->messages);
    //$this->partialLoop('partials/messages.phtml', $this->messages);
} ?>

Why is the message not output the first time it is set?

I have a feeling this is to do with the messenger being stored in sessions but I'm sure it's to do with my implementation.

When I submit a bad value to my form I don't get a message until I either send the form again or refresh.

What is a good way of sending this to the PartialLoop helper?

The output of the messenger is something like:

Array(
    [0] => 'Message',
    [1] => 'Second message' //etc.
)

But this is no good for a PartialLoop as I need to get the message (which needs each message to be an array, containing a 'message' => 'Message string' key/value pair).

Is there a better method instead of rewriting the array before submitting it to the view?

+1  A: 

Ill quote from the documentation.

10.8.4.4.1. Introduction

The FlashMessenger helper allows you to pass messages that the user may need to see on the next request. To accomplish this, FlashMessenger uses Zend_Session_Namespace to store messages for future or next request retrieval.

Basically, this means that yes you have to refresh the page to see the messages.

Edit for PartialLoop:

You could try this:

foreach ($array as $message) {
  $newArray[]['message'] = $message;
}

But you dont have to use PartialLoop. You could send it to Partial and loop it there. Or even loop it right there in your view.

OIS
+1  A: 

Referring to OIS I'd like to add that you can retrieve the flash-messages within the same request in which they where added to the FlashMessenger. In this case you'd have to use Zend_Controller_Action_Helper_FlashMessenger::getCurrentMessages().

In your case you'd have to change the line

$this->view->messages = $this->_messages->getMessages();

to

$this->view->messages = $this->_messages->getCurrentMessages();

Hope that helps.

Stefan Gehrig
A: 

I am haveing the same issue, but unfortunatelly

Zend_Controller_Action_Helper_FlashMessenger::getCurrentMessages()

does not help :(