I have a custom save function in a class that is extending JModelAdmin.

The save function calls several external functions in other helper files.

At several points those helper files can indicate that there is an error and do a 'return false' which then causes the save to fail. That is working.

When this happens Joomla nicely displays

"Save failed with the following error: "

at the top of the form.

What I'm not sure is how to properly get the 'reason' back to the parent save function so that it will display the error notice after that notice.

So far the only thing that I've been able to do is within the helper file add the line:

JFactory::getApplication()->enqueueMessage(JText::sprintf('Your %s file is not the proper format type.',$typeName),'error');

right before the

return false;

line within the helper file.

The problem is that this is displayed ABOVE the save failure notice, resulting in

Your image file is not the proper format type.

Save failed with the following error:

Which is in the wrong order.

Is there a way that I can 'push' the error type back to the calling save function (and back to the base save function) and display my error in the proper order UNDER the 'following error:' message?

1 Answer 1


The way JFactory::getApplication()->enqueueMessage() display the messages in the same sequence it gets it. So "your image..." message was added first and so it shows first. So the short answer is for the errors to line up you should add them in the correct order.

There are two other ways I have found to circumvent this for now.

One is to use my own error gathering helper method and then at last bump them into the JFactory::getApplication()->enqueueMessage() in the correct order. Here is the code used in the helper class.

*   The errors
protected static $errors = array();

public static function getError($key)
    if (isset(self::$errors[$key]) && self::checkArray(self::$errors[$key]))
        return '<ul><li>'.implode('</li><li>', self::$errors[$key]).'</li><ul>';
    return false;

protected static function setError($key, $errorArray)
    if (self::checkArray($errorArray))
        foreach ($errorArray as $error)
            if (!isset(self::$errors[$key]))
                self::$errors[$key] = array();
            self::$errors[$key][] = $error;

Then in the view you can do something like this:

if ($errors = YourComponentHelper::getError(1))
    JFactory::getApplication()->enqueueMessage('<h1>Save failed with the following error:</h1>'.$errors, 'Error');

The second option is that your model class has a function to get errors from it, and give it to the current model.

So in that case you can do something like this:

$model = YourComponentHelper::getModel('expert', JPATH_COMPONENT_ADMINISTRATOR); // A JCB custom helper method
if ($model->save($data))
    return true;
$errors = $model->getErrors();
if (YourComponentHelper::checkArray($errors)) // also a JCB custom helper method
    foreach ($errors as $error)

But again you will have to insure that you load these in the correct order.

  • I'm still a bit 'new' on the 'static' designation. But if this helper class were being called from multiple users all hitting the same functions, wouldn't there be a problem with the errors being crossed (i.e. user 1 getting the error for user 2 if they happen to be using the same helper functions and then call the getErrors in the wrong order? Commented Jun 15, 2018 at 15:33
  • Or is the static state directly tied to a session? Sorry if these are basic php issues. I've tried to find explanation to determine how this would work with multiple users, but can't find anything that explains that clearly. Commented Jun 15, 2018 at 15:36
  • You can be sure that the errors will not be shared between users. Little much to explain. short answer is, static values in a static class is only static within the current execution stack of code. Once the result is given to the browser those values are not being remembered. You can run a few test with static concepts here. Open the page in two browsers and you will see it clearly does not effect each other. You can can copy the example to your own dev-environment and test it there for absolute certainty.
    – Llewellyn
    Commented Jun 15, 2018 at 19:36
  • Thanks. I use this with some minor modifications and seems to be working. Commented Jun 19, 2018 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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