[SalesForce] Show error message in Visualforce Page

I have a visualforce page with custom controller. I have added apex:pagemessages in a helper static class and for some reason the messages are not displaying on my Visualforce page and I'm not sure if this is a best practice but I have lots of validations to take care instead of doing in the controller I have moved all the code to a helper class.

Helper class for validations:

public with sharing class ValidationHelper 
{
    public static boolean doValidation(myCustomObject acc)
    {
          //just few as an sample
          boolean b = false;
          if(acc.name == '' || acc.name == null)
          {
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Account name'));
           b = true;
          }
          if(acc.AccountNumber == '' || acc.AccountNumber == null)
          {
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter Account number'));
           b = true;
          }
       return b;
    }
}

Controller Class:

Boolean isInValid = ValidationHelper.doValidation(acc); 
if(isInValid) { return null; } 

even though my isInValid is true but I'm not seeing any error messages that I'm capturing in doValidation method

Visualforce page:

<apex:form id="frm">
    <apex:pageMessages id="pageMsg"/> 
    <apex:pageblock title="Detail" mode="mainDetail" >  
        <apex:commandButton value="Submit" action="{!Submitview}" reRender="frm,pageMsg" />
    </apex:pageBlock>
</apex:form>

How can I display the error messages that I'm capturing in Helper class and show it on VFP?

Best Answer

You need to specify id to <apex:pageMessages id="pageMsg"/>

Specify this id in each reRender of action

Like

reRender="pageMsg"
Related Topic