Mehmet Davut
Mehmet Davut

Reputation: 667

Type of Flash Messenger in Zend

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');

Upvotes: 15

Views: 9751

Answers (4)

David
David

Reputation: 1674

Method signatures in Zend Framework 1.12.x for FlashMessenger:

public function addMessage($message, $namespace = null)
public function getMessages($namespace = null)
public function hasMessages($namespace = null)
public function clearMessages($namespace = null)

So to set messages the following will work:

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

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

And for the view, the following should work:

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

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

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

Upvotes: 0

rafeca
rafeca

Reputation: 272

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

Upvotes: 23

takeshin
takeshin

Reputation: 50648

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');
      }
    . . .
  }
}

Upvotes: 3

prodigitalson
prodigitalson

Reputation: 60413

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'));

Upvotes: 3

Related Questions