Dear all,

I have a couple of questions related to ZF's FlashMessenger and Controller plugins.

I have 2 type of messengers, which both extend the FlashMessenger. The first one is the ResultMessenger, which I use to notify the user when an action has succeeded. The other one is the ErrorMessenger. This one is used to collect error messages, and notify the user when a certain action has failed.

The code of these classes is pretty simple, as they just change the default namespace:

ResultMessenger:
class Sanmax_Controller_Action_Helper_ResultMessenger extends Zend_Controller_Action_Helper_FlashMessenger
{
   /**
    * $_namespace
    *
    * @var string
    */
   protected $_namespace = 'error';
}

Usage:
$this->_helper->ResultMessenger->addMessage($this->_tmx->_('common.item.saved'));

ErrorMessenger:
class Sanmax_Controller_Action_Helper_ErrorMessenger extends Zend_Controller_Action_Helper_FlashMessenger
{
   /**
    * $_namespace
    *
    * @var string
    */
   protected $_namespace = 'error';
}

Usage (from within a model):
$error = Zend_Controller_Action_HelperBroker::getExistingHelper('ErrorMessenger');
$error->addMessage($tmx->_('error.accompanimentaims.accompaniment_aim'));

I have also written a small controller plugin, which will automaticly assign the result and error messages to the view:

class Popovggz_Controller_Plugin_Messages extends Zend_Controller_Plugin_Abstract
{
   public function preDispatch()
   {
$view = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->view; $errors = Zend_Controller_Action_HelperBroker::getStaticHelper('errorMessenger')->getMessages(); $result = Zend_Controller_Action_HelperBroker::getStaticHelper('resultMessenger')->getMessages();

       $view->messages = array(
           'result' => $result,
           'error'  => $errors
       );
   }
}

This plugin is registered into the frontcontroller as follows:
$frontController->registerPlugin(new Popovggz_Controller_Plugin_Messages());

The problem which I am now facing, is that the messages do not show up until the 2nd request.

Take for instance the following code:

class AccompanimentAimsController extends Zend_Controller_Action
{
   public function addAction()
   {
       // snip
       if ($this->_request->isPost()) {
           // snip
            // the validate method adds messages to to ErrorMessages
            // when certain errors occur, and returns false if there are
            // messages in the current error namespace.
           if (!$tslModel->validate()) {
               return;
           }
       }
   }
}

The error messages do not show up, until I press the submit button for the 2nd time, or go to a different controller/action.

Any ideas/suggestions?

Best,


--
Andries Seutens
http://andries.systray.be

Gecontroleerd op virussen door de JOJO Secure Gateway.

Reply via email to