Actions

Difference between revisions of "Display error messages and notices"

From Joomla! Documentation

m (Tidied up a little. Added categorisation.)
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();  
 
$app =& JFactory::getApplication();  
 
//add a message to the message queue
 
//add a message to the message queue
$app->enqueueMessage(JText::_('Random error occured'), 'error');
+
$app->enqueueMessage( JText::_( 'Some error occurred' ), 'error' );
  
/** PHP 5 */
+
/** Alternatively, in PHP 5 */
JFactory::getApplication()->enqueueMessage(JText::_('Random error occured'), 'error');
+
JFactory::getApplication()->enqueueMessage( JText::_( 'Some error occurred' ), 'error' );
 
</source>
 
</source>
  
The second argument to the enqueueMessage()-function is the type of message. Default is 'message', but 'error' gives a different style message. Try other types as well, to see how that works. The api for the JApplication-object can be found at:
+
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.
[http://api.joomla.org/Joomla-Framework/Application/JApplication.html JApplication API]
+
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 lang="php">
 
<source lang="php">
 
<jdoc:include type="message" />
 
<jdoc:include type="message" />
Line 25: Line 23:
 
/** PHP 4 */
 
/** PHP 4 */
 
$app = JFactory::getApplication()
 
$app = JFactory::getApplication()
$app->enqueueMessage('Message');
+
$app->enqueueMessage( 'Message' );
 
+
/** PHP 5 */
+
JFactory::getApplication()->enqueueMessage('Message');
+
  
 +
/** Alternatively, in PHP 5 */
 +
JFactory::getApplication()->enqueueMessage( 'Message' );
 
</source>
 
</source>
  
Line 38: Line 35:
 
</div>
 
</div>
 
<source lang="php">
 
<source lang="php">
JError::raiseNotice(100, 'Notice');
+
JError::raiseNotice( 100, 'Notice' );
 
</source>
 
</source>
  
Line 47: Line 44:
 
</div>
 
</div>
 
<source lang="php">
 
<source lang="php">
JError::raiseWarning(100, 'Warning');
+
JError::raiseWarning( 100, 'Warning' );
 
</source>
 
</source>
  
 
== Error ==
 
== Error ==
 
<source lang="php">
 
<source lang="php">
JError::raiseError(4711, 'A severe error occured');
+
JError::raiseError( 4711, 'A severe error occurred' );
 
</source>
 
</source>
  
 
== See also ==
 
== See also ==
 
* [[JError]]
 
* [[JError]]
 +
* [[JApplication]]
 +
*  [http://api.joomla.org/Joomla-Framework/Application/JApplication.html JApplication on api.joomla.org]
 +
 +
<noinclude>[[Category:Development]]</noinclude>

Revision as of 13:39, 3 November 2009

Contents

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
$app =& JFactory::getApplication(); 
//add a message to the message queue
$app->enqueueMessage( JText::_( 'Some error occurred' ), 'error' );
 
/** Alternatively, in PHP 5 */
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

Message

/** PHP 4 */
$app = JFactory::getApplication()
$app->enqueueMessage( 'Message' );
 
/** Alternatively, in PHP 5 */
JFactory::getApplication()->enqueueMessage( 'Message' );

Notice

Notice

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

Warning

Warning

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

Error

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

See also