views:

57

answers:

3

Is it possible or How can i give a type to a FlashMessage in Zend?

For example

/* This is a "Success" message */
$this -> _helper -> FlashMessenger('You are successfully created a post.'); 

/* This is an "Error" message  */
$this -> _helper -> FlashMessenger('There is an error while creating post.');

/* This is just a "Notification" message */
$this -> _helper -> FlashMessenger('Now you can see your Post');
A: 

At one time you used assoc arrays to do this... Im not ure if this is still current or not...

/* This is a "Success" message */
$this -> _helper -> FlashMessenger(array('success' => 'You are successfully created a post.')); 

/* This is an "Error" message  */
$this -> _helper -> FlashMessenger(array('error' => 'There is an error while creating post.'));

/* This is just a "Notification" message */
$this -> _helper -> FlashMessenger(array('notice' => 'Now you can see your Post'));
prodigitalson
A: 

It is possible. Sample implementation in described in this blog post:

Excerpt:

class AuthController extends Zend_Controller_Action {
  function loginAction() {
    . . .
    if ($this->_request->isPost()) {
      $formData = $this->_request->getPost();
      if ($this->view->form->isValid($formData)) {
        . . .
      } else {
        $this->view->priorityMessenger('Login failed.', 'error');
      }
    . . .
  }
}
takeshin
+1 Beat me to it.
David Weinraub
+1  A: 

I think the best way to do this is by using the flashmessenger namespaces:

/* success message */
$this->_helper->FlashMessenger()->setNamespace('success')->addMessage('Post created!');

/* error message */
$this->_helper->FlashMessenger()->setNamespace('error')->addMessage('You have no permissions');

And then in your layout you can get the messages added to each namespace:

<?php $flashMessenger = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');

<?php if ($flashMessenger->setNamespace('success')->hasMessages()): ?>
    <div class="message success">
    <?php foreach ($flashMessenger->getMessages() as $msg): ?>
        <?php echo $msg ?>
    <?php endforeach; ?>
    </div>
<?php endif; ?>

<?php if ($flashMessenger->setNamespace('error')->hasMessages()): ?>
    <div class="message error">
    <?php foreach ($flashMessenger->getMessages() as $msg): ?>
        <?php echo $msg ?>
    <?php endforeach; ?>
    </div>
<?php endif; ?>
rafeca
A good solution also
David Caunt
This is good but, selected was shorter.
davit
ok, the selected solution is some chars shorter, but IMHO this one is a little bit better: in the ZF documentation it's said that addMessage() should get a string as a parameter, so sending an array even though it works is discouraged. Also when adding multiple messages to the flashMessenger with the first solution is harder to display them in the views (you'll have to do some array filtering)
rafeca
@davit: Shorter does not always mean better.
takeshin
Yes, you' re right guys, shorter is not always better, i select it because, it solved my problem more quickly, so i accepted it my personal opinion. But i should change selected Answer, because right answer is that answer. Sorry.
davit