I'm writing VF page and I'm curious. What is the best way to show error message on page from RemoteAction? Should I just create some <div> and set to it's error message or there is way to set text to <apex:messages> from JS?
1 Answer
There are some solutions for that. I am using my own framework for showing error messages from the @RemoteAction methods. It is based on the HTML, JavaScript and Bootstrap. If some error occurrs, then I show the message text in the popup window. Bootstrap is just a nice helper framework, it is not necessarily required.
Here is a javascript function snippet that calls a RemoteAction method. If an event status have some message - it should be displayed to the user:
ClassName.remoteMethode(param, function(result, event)
{
if(event.status)
{
// All is OK
}
else
{
// Error occurred
jQuery('#errorText').text(event.message);
jQuery('#errorDialog').modal();
}
}
And here is an HTML-popup based on the bootstrap example (you could create your own structure based on the requirement):
<div class="modal" id="errorDialog">
<div class="modal-dialog" style="top: 35%;">
<div class="modal-content" style="text-align: center;">
<div class="modal-header">
<span class="glyphicon glyphicon-warning-sign" style="font-size:26px; color:orange;margin: 0 10px 0 0;vertical-align: bottom;" aria-hidden="true"></span>
Error occurred!
</div>
<div class="modal-body">
<div id="errorText"></div>
</div>
<div class="modal-footer" style="text-align: center;">
<button id="scanButton" class="btn btn-warning" type="button" onclick="jQuery('#errorDialog').modal('hide');">Close</button>
</div>
</div>
</div>
</div>
And the result looks like this then:
-
Thanks, nice solution with style) Sad, that it's not possible to add messages to standard VF message element– ytiqCommented Feb 16, 2015 at 12:59
-
I've just used the technique from the SFDC examples: salesforce.com/docs/developer/pages/Content/… Commented Feb 16, 2015 at 13:00
-
@ytiq I don't think it is not possible. Maybe yo could create a new Message in the RemoteAction method and on the page if calling this method just rerender a pagemessage block on the same time using some actionFunction. Commented Feb 16, 2015 at 13:02
-
I think I just came up with solution, we can add ID to VF element, and then in add JS using '{!$Component.theMessages}' append error text to it.– ytiqCommented Feb 16, 2015 at 13:06
-
1@ytiq I would not reccoemnt to use reRender together with Javascript Remoting. If you want to have a look & feel of the standard SFDC error message - just style some html element so it looks like thiat message. Otherwise you are losing the meaning of the javascript remoting - you will get more on the ViewState and you will have to reRender some panels - more loading time... Commented Feb 16, 2015 at 13:16