Working with the Zend Framework FlashMessenger and jQuery UI Language Classes

I have a Zend Framework application that uses jQuery UI.

In my controllers, I set error / success messages using the FlashMessenger assistant as follows:

// ExampleController.php $this->_helper->FlashMessenger('Sorry, could not complete your request.'); 

In my layout, I display messages using the Noumenal FlashMessenger View Assistant

 // layout.phtml <?php echo $this->flashMessenger(); ?> 

I want to use jQuery UI CSS styles to style my error messages as follows:

 <div class="ui-state-error ui-corner-all"> <p><span class="ui-icon ui-icon-alert"></span> <strong>Alert:</strong> Sample ui-state-error style.</p> </div> 

... but the view helper does all the work, so I cannot change the classes as I want. Therefore, before going to a dead end, I thought that I would ask the community. Here are my questions:

  • How to use Zend Framework FlashMessenger so that I can set different messages depending on the state (error / success)?
  • How can I get messages from FlashMessenger and display them in one place without having to duplicate the code in all my controllers?
  • How can I derive a different class for each of the different message states? For example: 'error'=>'ui-state-error', 'info'=>'ui-state-highlight' , etc.
+4
css jquery-ui zend-framework
source share
2 answers

By writing the Noumenal FlashMessenger View Helper Helper , I have to help. :-)

To answer your question:

Adding Messages

You can set different message levels, for example. error , warning , etc., passing the array to the FlashMessenger action helper, rather than a simple line:

 // ExampleController.php $this->_helper->FlashMessenger( array('error'=>'Sorry, could not complete your request.') ); 

The view helper is designed to recognize this.

Message output

When FlashMessages is displayed, your layout has additional parameters that you can pass to indicate the default message level (which is warning by default) and the template for your message.

Adapting a code fragment to account for different levels of messages, you can achieve the desired result by making the following call in your layout:

 // layout.phtml $template = '<div class="ui-state-error ui-corner-all"> <p class="%s"><span class="ui-icon ui-icon-alert"></span> <span class="flash-message">%s</span></p> </div>'; echo $this->flashMessenger('error', $template); 

(Perhaps you should set the template as a variable of the form, for example, in your boot file.)

By doing this, the view assistant will create the appropriate formatted flash messages for you as you wish.

Simple style

With CSS, there will be enough space for writing messages correctly. For example:

 .alert { color: red; } .alert .flash-message:before { content: "<strong>Alert</strong> "; } .notice { color:yellow; } .notice .flash-message:before { content: "<strong>Notice</strong> "; } 

I leave you to improvise ...

I wrote a guide for Zend Framework FlashMessenger and a view helper on my blog . Perhaps read this. Also, please write to me to inform me of your difficulties - this will help me find out what I need to improve.

I hope this helps.

+9
source share

I have finished modifying the flashMessenger() view flashMessenger() to use my jQuery templates. It displays the correct key-based template (error, notification, etc.). This is probably the best way to do this, but it is doing its job.

 public function flashMessenger(...) { //... //process messages foreach ($messages as $message) { if (is_array($message)) { list($key,$message) = each($message); } $template = $this->_getJqueryTemplate($key); $output .= sprintf($template,$message); } return $output; } private function _getJqueryTemplate($messageLevel) { switch($messageLevel) { case 'error': $template = ' <div class="ui-state-error ui-corner-all" style="padding: 0pt 0.7em;"> <p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: 0.3em;"></span> %s</p> </div>'; break; default: $template = ' <div class="ui-state-highlight ui-corner-all" style="padding: 0pt 0.7em;"> <p><span class="ui-icon ui-icon-info" style="float: left; margin-right: 0.3em;"></span> %s</p> </div>'; } return $template; } 

Then, in the controller, specify what type of message you want to send, passing it as an array key.

 // ExampleController.php $this->_helper->FlashMessenger( array('error'=>'Sorry, could not complete your request.') ); 
+1
source share

All Articles