Difference between revisions of "Display error messages and notices"

From Joomla! Documentation

m (Added alerts - adds a contextual keyword for a synonym of messages.)
(Several markup changes. URL corrections.)
 
Line 3: Line 3:
 
Errors, warnings, alerts and notices can be displayed from any component, module, plugin or template using the methods outlined below.</translate>
 
Errors, warnings, alerts and notices can be displayed from any component, module, plugin or template using the methods outlined below.</translate>
  
<source lang="php">
+
<syntaxhighlight lang="php">
 
// Get a handle to the Joomla! application object
 
// Get a handle to the Joomla! application object
 
$application = JFactory::getApplication();
 
$application = JFactory::getApplication();
Line 12: Line 12:
 
/** Alternatively you may use chaining */
 
/** Alternatively you may use chaining */
 
JFactory::getApplication()->enqueueMessage(JText::_('SOME_ERROR_OCCURRED'), 'error');
 
JFactory::getApplication()->enqueueMessage(JText::_('SOME_ERROR_OCCURRED'), 'error');
</source>
+
</syntaxhighlight>
  
 
<translate><!--T:2-->
 
<translate><!--T:2-->
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.</translate>
+
The second argument to the [https://api.joomla.org/cms-4/classes/Joomla-CMS-Application-CMSWebApplicationInterface.html#method_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 ''jdoc:include'' statement in your template. Place the following in your template at the location where you want messages to appear.</translate>
  
<source lang="php">
+
<syntaxhighlight lang="php">
 
<jdoc:include type="message" />
 
<jdoc:include type="message" />
</source>
+
</syntaxhighlight>
  
 
<translate>
 
<translate>
Line 28: Line 28:
 
Message
 
Message
 
</div>
 
</div>
<source lang="php">
+
<syntaxhighlight lang="php">
 
JFactory::getApplication()->enqueueMessage('Message');
 
JFactory::getApplication()->enqueueMessage('Message');
</source>
+
</syntaxhighlight>
  
 
<translate>
 
<translate>
Line 39: Line 39:
 
Notice
 
Notice
 
</div>
 
</div>
<source lang="php">
+
<syntaxhighlight lang="php">
 
JError::raiseNotice( 100, 'Notice' );
 
JError::raiseNotice( 100, 'Notice' );
</source>
+
</syntaxhighlight>
  
 
<translate>
 
<translate>
Line 50: Line 50:
 
Warning
 
Warning
 
</div>
 
</div>
<source lang="php">
+
<syntaxhighlight lang="php">
 
JError::raiseWarning( 100, 'Warning' );
 
JError::raiseWarning( 100, 'Warning' );
</source>
+
</syntaxhighlight>
  
 
<translate>
 
<translate>
Line 61: Line 61:
 
Error
 
Error
 
</div>
 
</div>
<source lang="php">
+
<syntaxhighlight lang="php">
 
JError::raiseError( 4711, 'A severe error occurred' );
 
JError::raiseError( 4711, 'A severe error occurred' );
</source>
+
</syntaxhighlight>
  
 
<translate>
 
<translate>
== Joomla! 3.x is bootstrapped == <!--T:7-->
+
== Joomla! 3.x is Bootstrapped == <!--T:7-->
 
</translate>
 
</translate>
 
<translate><!--T:8-->
 
<translate><!--T:8-->
Since Joomla! {{JVer|3.x}} uses bootstraped templates, the messages will use the standard bootstrap CSS styles for Alerts.</translate>
+
Since Joomla! {{JVer|3.x}} uses Bootstrapped templates, the messages will use the standard Bootstrap CSS styles for Alerts.</translate>
  
 
<translate><!--T:9-->
 
<translate><!--T:9-->
See: http://getbootstrap.com/2.3.2/components.html#alerts</translate>
+
See: https://getbootstrap.com/2.3.2/components.html#alerts</translate>
  
 
<translate><!--T:10-->
 
<translate><!--T:10-->
 
The general syntax remains:</translate>
 
The general syntax remains:</translate>
  
<source lang="php">
+
<syntaxhighlight lang="php">
 
JFactory::getApplication()->enqueueMessage('Your Message', 'type');
 
JFactory::getApplication()->enqueueMessage('Your Message', 'type');
</source>
+
</syntaxhighlight>
  
 
<translate><!--T:11-->
 
<translate><!--T:11-->
Where '''type''' can be one of</translate>
+
Where ''type'' can be one of</translate>
  
 
<translate><!--T:12-->
 
<translate><!--T:12-->
Line 92: Line 92:
  
 
<translate>
 
<translate>
== See also == <!--T:13-->
+
== See Also == <!--T:13-->
 
</translate>
 
</translate>
 
<translate><!--T:14-->
 
<translate><!--T:14-->
* [http://api.joomla.org/11.4/Joomla-Platform/Error/JError.html JError on api.joomla.org]
+
* [https://api.joomla.org/cms-3/classes/JError.html JError on api.joomla.org]
* [http://api.joomla.org/11.4/Joomla-Platform/Application/JApplication.html JApplication on api.joomla.org]
+
* [https://api.joomla.org/cms-3/classes/JApplication.html JApplication on api.joomla.org]
 
</translate>
 
</translate>
 
__NOTOC__
 
__NOTOC__

Latest revision as of 17:00, 29 November 2022

Other languages:
Deutsch • ‎English • ‎Nederlands • ‎español • ‎français

Errors, warnings, alerts 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]

Error

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

Joomla! 3.x is Bootstrapped[edit]

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

See: https://getbootstrap.com/2.3.2/components.html#alerts

The general syntax remains:

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

Where type can be one of

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

See Also[edit]