0

In my componenent on Joomla! 4, when i do an ajax request and that ajax request return an error or exception, i'am wondering how to display it into enqueuemessage.

Is-there a way to do it ?

Thanks by advance

  • May we see your ajax processing script(s)? Are there particular errors that you expect to encounter? – mickmackusa Dec 29 '19 at 23:29
2

When you say "display it into enqueuemessage" I'm assuming that you want to display it in the same message area that enqueueMessage displays in. You can do that in your javascript by calling Joomla.renderMessages(), which is a function in the Joomla core.js source file.

The easiest way to get it to work is to use the JResponseJson class to handle the JSON response. See https://docs.joomla.org/JSON_Responses_with_JResponseJson for a description.

So in your Ajax handling server code:

use Joomla\CMS\Factory;
$app = Factory::getApplication();  
$app->enqueueMessage("Enqueued notice", "notice");
echo new JResponseJson("some response data", "some response message", false, false);

Provided the 4th parameter ($ignoreMessages in the API) is set to false (and it is false by default), JResponseJson will copy the enqueued messages into the Ajax response (and clear them from the session).

The above will result in an Ajax response

{"success":true,"message":"some response message","messages":{"notice":["Enqueued notice"]},"data":"some response data"}

Then in your javascript code which handles the Ajax you can do something like:

jQuery.ajax({   
    … // your request data
    })
    .done(function(result, textStatus, jqXHR)
    {
        ...
        Joomla.renderMessages(result.messages);
    });

You can find the code of a sample component demonstrating this at https://docs.joomla.org/JSON_Responses_with_JResponseJson#Sample_Component_Code. I verified that this worked on my Joomla4 instance.

If you don't use JResponseJson, then provided you've got the messages section of the Ajax response JSON-encoded in the same way as JResponseJson does, then you can obviously still use Joomla.renderMessages() on it.

  • Thanks it's something like that, except that i want to render the error/exception message into custom place but it seems that Joomla.renderMessages() accepts to put it into our own selector – user3533235 Jan 10 at 10:36
  • The error strings when it's displayed, isn't translated : if (data.result.message) { const messages = { error: [data.result.message], }; Joomla.renderMessages(messages, '#mycomponent-message-container'); } – user3533235 Jan 11 at 17:21
  • When the original enqueueMessage is called, it should really be passed a JText literal, like $app->enqueueMessage(JText::_('JLIB_DATABASE_ERROR_DATABASE_CONNECT'), "error");. The message should then appear translated ok. – Robbie Jackson Jan 11 at 20:51

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.