function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sandhya NarayananSandhya Narayanan 

validation rule error msg not getting displayed on VF page

I have a few validation rules defined on custom fields of account object. These fields are getting fetched on a VF page, but the validation rule error messages are not getting displayed on the page. I have added the tag  <apex:messages /> in my VF page, still the error msg is not getting displayed.
Kindly advise what i should do so that the validation rule error msg gets displayed on the VF page.

Thanks,
Sandhya
veda Hebbarveda Hebbar
Hi,


Validation rule will fire only when your performing some DML operations. Please check you rerender apex:messages ID.

Do you have controller apex class for vf page?

If yes, on click of button in controller Catch block add ApexPages.addMessages() and rerender apex:messages ID.

method(){

    try{
        // your logic
    }
    catch(Exception ex){
        ApexPages.addMessages(ex);
        return null;
    }


}

Thanks,
​Veda
Srinivas SSrinivas S
If you are using custom controller for the page -
Please keep your DML operations in 
try {
	//your DML operation
}
Catch(Exception e) {
	ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.Error,e.getMessage()));
}

Please use try using -
<apex:pageMessages id="pgMsg"/>

You should be able to see the errors.
Note: make sure to rerender "pgMsg".

Thanks,
Srinivas
- Please mark as solution if your problem is resolved.
Sandhya NarayananSandhya Narayanan
Thank You for the response Veda and Srinivasa.

I have kept my DML operation inside try catch block itself. When i checked in the debug log, the error msg is coming as well.
This is how it is coming in the debug log:
VF_PAGE_MESSAGE| data is invalid
But this is not getting displayed on the VF page.

I tried rerending the apex:pagemessage on button click, still its not working.

My save button calls a function in controller, which inturn calls another function in the class and it is here that the exception is occuring.

 
veda Hebbarveda Hebbar
Hi Sandhya Narayanan,

Can you please try below steps.
1. Create on string variable in controller to store exception value.
2. set that variable with 'ApexPages.severity.Error,e.getMessage()' value in catch block.
3. In Parent method which is called by directly from page(as you have mentioned above save button is calling one more method where you are getting exception.) after function call check if the string is not blank. If string is not blank, add page message.

Example:
String strException ;
public controller()
{
    strException = '';
}
public methodCalledOnClickOfSave()
{
    //your logic
    callingAnotherMethod();
    if(strException.isNotBlank)
    {
        ApexPages.addMessage(new ApexPages.Message(strException));
    }
}

private callingAnotherMethod()
{
    try
    {
        //your logic
    }
    Catch(Exception e) {
        strException = ApexPages.severity.Error,e.getMessage();
    }

}
veda Hebbarveda Hebbar
Hi Sandhya,

Are you able to solve the issue? Please let me know if you need more information or help on this.

Thanks,
Vedashri
Sandhya NarayananSandhya Narayanan
Hi Vedashri,

Thanks for your help.
I was not able to find a solution for this, so I had to go with writing custom validation in the controller rather than the standard validation. The function which we were calling in the 'Save' button was overriding the standard validations from being displayed on the page.