Difference between revisions of "Display error messages and notices"

From Joomla! Documentation

(New page: Errors, warnings and notices can be displayed from any component, module, plugin or template using the method outlined below (took me some time to figure this out, might help someone else)...)
 
(Update 3.0 to 3.x)
(15 intermediate revisions by 9 users not shown)
Line 1: Line 1:
Errors, warnings and notices can be displayed from any component, module, plugin or template using the method outlined below (took me some time to figure this out, might help someone else):
+
{{RightTOC}}
 +
Errors, warnings and notices can be displayed from any component, module, plugin or template using the methods outlined below.
 +
<source lang="php">
 +
// Get a handle to the Joomla! application object
 +
$application = JFactory::getApplication();
  
//Get a handle to the Joomla!-application object
+
// Add a message to the message queue
$app =& JFactory::getApplication();
+
$application->enqueueMessage(JText::_('SOME_ERROR_OCCURRED'), 'error');
//add a message to the message queue
 
$app->enqueueMessage(JText::_('Random error occured'), 'error');
 
  
The second argument to the enqueueMessage()-function is the type of message. Default is 'message', but both error, and I believe also 'warning' will work. The api for the JApplication-object can be found at:
+
/** Alternatively you may use chaining */
[[http://api.joomla.org/Joomla-Framework/Application/JApplication.html]]
+
JFactory::getApplication()->enqueueMessage(JText::_('SOME_ERROR_OCCURRED'), 'error');
The error message will now be displayed if the message-tag is present in your template. The message-field is added with the following statement in your template:
+
</source>
  
 +
The second argument to the [[JApplication/enqueueMessage|enqueueMessage]] function is the type of the message. The default is 'message', but 'error' results in a different style for the message.  The message will be displayed in place of a special <tt>jdoc:include</tt> statement in your template.  Place the following in your template at the location where you want messages to appear.
 +
<source lang="php">
 
<jdoc:include type="message" />
 
<jdoc:include type="message" />
 +
</source>
 +
 +
== Message ==
 +
<div style="background-color: #c3d2e5; color: #0055bb; border-top: 3px solid #84a7db; border-bottom: 3px solid #84a7db; padding-left: 1em; font-weight: bold;">
 +
Message
 +
</div>
 +
<source lang="php">
 +
JFactory::getApplication()->enqueueMessage('Message');
 +
</source>
 +
 +
== Notice ==
 +
 +
<div style="background-color: #efe7b8; color: #c00; border-top: 3px solid #f0dc7e; border-bottom: 3px solid #f0dc7e; padding-left: 1em; font-weight: bold;">
 +
Notice
 +
</div>
 +
<source lang="php">
 +
JError::raiseNotice( 100, 'Notice' );
 +
</source>
 +
 +
== Warning ==
 +
 +
<div style="background-color: #e6c0c0; color: #c00; border-top: 3px solid #DE7A7B; border-bottom: 3px solid #DE7A7B; padding-left: 1em; font-weight: bold;">
 +
Warning
 +
</div>
 +
<source lang="php">
 +
JError::raiseWarning( 100, 'Warning' );
 +
</source>
 +
 +
== Error ==
 +
<source lang="php">
 +
JError::raiseError( 4711, 'A severe error occurred' );
 +
</source>
 +
 +
== Joomla! 3.x is bootstraped ==
 +
Since Joomla! {{JVer|3.x}} uses bootstraped templates, the messages will use the standard bootstrap CSS styles for Alerts.
 +
 +
See: http://twitter.github.com/bootstrap/components.html#alerts
 +
 +
The general syntax remains:
 +
 +
<source lang="php">
 +
JFactory::getApplication()->enqueueMessage('Your Message', 'type');
 +
</source>
 +
 +
Where '''type''' can be one of
 +
 +
* 'warning' - yellow
 +
* 'notice' - blue
 +
* 'error' - red
 +
* 'message' (or empty) - green
 +
 +
== See also ==
 +
* [[JError]]
 +
* [[JApplication]]
 +
*  [http://api.joomla.org/1.5/Joomla-Framework/Application/JApplication.html JApplication on api.joomla.org]
 +
 +
<noinclude>[[Category:Development]]</noinclude>
 +
[[Category:Tutorials]][[Category:Component Development]]

Revision as of 04:34, 29 April 2013

Errors, warnings and notices can be displayed from any component, module, plugin or template using the methods outlined below.

// Get a handle to the Joomla! application object
$application = JFactory::getApplication();

// Add a message to the message queue
$application->enqueueMessage(JText::_('SOME_ERROR_OCCURRED'), 'error');

/** Alternatively you may use chaining */
JFactory::getApplication()->enqueueMessage(JText::_('SOME_ERROR_OCCURRED'), 'error');

The second argument to the enqueueMessage function is the type of the message. The default is 'message', but 'error' results in a different style for the message. The message will be displayed in place of a special jdoc:include statement in your template. Place the following in your template at the location where you want messages to appear.

<jdoc:include type="message" />

Message[edit]

Message

JFactory::getApplication()->enqueueMessage('Message');

Notice[edit]

Notice

JError::raiseNotice( 100, 'Notice' );

Warning[edit]

Warning

JError::raiseWarning( 100, 'Warning' );

Error[edit]

JError::raiseError( 4711, 'A severe error occurred' );

Joomla! 3.x is bootstraped[edit]

Since Joomla! Joomla 3.x uses bootstraped templates, the messages will use the standard bootstrap CSS styles for Alerts.

See: http://twitter.github.com/bootstrap/components.html#alerts

The general syntax remains:

JFactory::getApplication()->enqueueMessage('Your Message', 'type');

Where type can be one of

  • 'warning' - yellow
  • 'notice' - blue
  • 'error' - red
  • 'message' (or empty) - green

See also[edit]