Difference between revisions of "Display error messages and notices"

From Joomla! Documentation

m (added missing ';' to PHP4 enqueueMessage example)
(Update links and remove red links)
(9 intermediate revisions by 5 users not shown)
Line 2: Line 2:
 
Errors, warnings and notices can be displayed from any component, module, plugin or template using the methods outlined below.
 
Errors, warnings and notices can be displayed from any component, module, plugin or template using the methods outlined below.
 
<source lang="php">
 
<source lang="php">
//Get a handle to the Joomla! application object
+
// Get a handle to the Joomla! application object
$app =& JFactory::getApplication();
+
$application = JFactory::getApplication();
//add a message to the message queue
 
$app->enqueueMessage( JText::_( 'Some error occurred' ), 'error' );
 
  
/** Alternatively, in PHP 5 */
+
// Add a message to the message queue
JFactory::getApplication()->enqueueMessage( JText::_( 'Some error occurred' ), 'error' );
+
$application->enqueueMessage(JText::_('SOME_ERROR_OCCURRED'), 'error');
 +
 
 +
/** Alternatively you may use chaining */
 +
JFactory::getApplication()->enqueueMessage(JText::_('SOME_ERROR_OCCURRED'), 'error');
 
</source>
 
</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.
+
The second argument to the [http://api.joomla.org/11.4/Joomla-Platform/Application/JApplication.html#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">
 
<source lang="php">
 
<jdoc:include type="message" />
 
<jdoc:include type="message" />
Line 21: Line 22:
 
</div>
 
</div>
 
<source lang="php">
 
<source lang="php">
/** PHP 4 */
+
JFactory::getApplication()->enqueueMessage('Message');
$app = JFactory::getApplication();
 
$app->enqueueMessage( 'Message' );
 
 
 
/** Alternatively, in PHP 5 */
 
JFactory::getApplication()->enqueueMessage( 'Message' );
 
 
</source>
 
</source>
  
Line 51: Line 47:
 
JError::raiseError( 4711, 'A severe error occurred' );
 
JError::raiseError( 4711, 'A severe error occurred' );
 
</source>
 
</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 ==
 
== See also ==
* [[JError]]
+
* [http://api.joomla.org/11.4/Joomla-Platform/Error/JError.html JError on api.joomla.org]
* [[JApplication]]
+
*  [http://api.joomla.org/11.4/Joomla-Platform/Application/JApplication.html JApplication on api.joomla.org]
*  [http://api.joomla.org/Joomla-Framework/Application/JApplication.html JApplication on api.joomla.org]
 
  
 
<noinclude>[[Category:Development]]</noinclude>
 
<noinclude>[[Category:Development]]</noinclude>
 
[[Category:Tutorials]][[Category:Component Development]]
 
[[Category:Tutorials]][[Category:Component Development]]

Revision as of 15:11, 4 July 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]