3

The official CMS 3.4 API page of Joomla classifies the JError class and all its methods as deprecated, yet I couldn't find any alternative to output messages when i.e. there is an error after server side validation for a specific component etc... Are we meant to use those deprecated methods or are there other more recommended ways of displaying these useful error/warning messages?

7

You can use enqueueMessage to display errors, for example:

$user = JFactory::getUser();

if ($user->guest)
{
    $msg = 'You must be logged in';
    JFactory::getApplication()->enqueueMessage($msg, 'error');    // red 
    JFactory::getApplication()->enqueueMessage($msg, 'warning');  // yellow
    JFactory::getApplication()->enqueueMessage($msg, 'notice');   // blue
    JFactory::getApplication()->enqueueMessage($msg, 'message');  // green
}

If you wish to store errors in logs files, you can use JLog, like so:

jimport('joomla.log.log');

JLog::add(JText::_('JTEXT_ERROR_MESSAGE'), JLog::WARNING, 'jerror');

You can also use PHP exceptions to catch errors, like so:

// Perform a database query or something here
try
{
    // Try to get an object based on the results from the database
    $results = $db->loadObjectList();
}
catch (Exception $e)
{
    // Start crying cause it didn't work
    echo $e->getMessage();
}
  • that's really cool, but will the output contain any class so I can tailor the look and feel of the message box depending on the type of message? i.e. red for error, yellow for warning etc.. – Mg Gm Mar 14 '15 at 18:26
  • @MgGm - Yes, this is possible, I've updated my answer showing how to do this – Lodder Mar 14 '15 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.