i want to perform an action on onComplete event of commandbutton.I am calling javascript function ,in that function i want to know if there is any error displaying on visualforce page(Validation error).Can i implement this without using any controller.
4 Answers
I wonder if you can use the css class errorMsg
of the error block:
If this class exists on the page - there is an error:
<apex:page>
<script>
function checkIt(){
if (jQuery('.errorMsg').length > 0){
// Error exists
alert('There is an error on the page!');
}
else{
alert('No errors!');
}
}
</script>
<apex:commandButton reRender="none" oncomplete="checkIt();" value="Click me"/>
</apex:page>
Well, you can definitely call ApexPages.hasMessages() from your apex. There's even a version that lets you specify the severity (WARNING, ERROR, FATAL, INFO) that you're looking for.
http://www.salesforce.com/us/developer/docs/pages/Content/apex_System_ApexPages_instance_methods.htm
To use this in your javascript, you could make a controller property or method that returns this result, and then simply access it via mergefield. So for example, you might define a controller method:
public Boolean ErrorsPresent(){
return ApexPages.hasMessages(ApexPages.Severity.ERROR);
}
Then your Javascript might looks like
if({!ErrorsPresent}){
alert('Woah there, buddy! Looks like there may be a problem');
}
-
While that is an interesting solution, the OP specifically asked for something that did not require a controller. Commented Oct 2, 2013 at 19:57
-
Ahh, that's actually the exact problem I had been trying to solve when I stumbled across this post, but without the use of Javascript Commented Oct 3, 2013 at 20:50
Pages with a StandardController expose a property:
{!Messages}
Without the need for a custom controller or extensions, can do things like:
<apex:pageBlock rendered="{!ISNULL(Messages)}">
<!-- detailed troubleshooting instructions -->
</apex:pageBlock>
<apex:pageBlock rendered="{!NOT(ISNULL(Messages))}">
<!-- normal page functionality -->
</apex:pageBlock>
I found that adding '.empty' to the end was just getting stripped on save, but ISNULL worked fine, as in the following example:
rendered="{!ISNULL(messages)}"