1

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?

2
  • 1
    Regardless of where the addMessage is, the message should be added to the current page context. Can you edit your post to include the rerender information, such as the button/actionFunction used to rerender the pageMessages tag? Commented Jun 29, 2016 at 19:03
  • I have updated my question.
    – Nick
    Commented Jun 29, 2016 at 20:03

2 Answers 2

2

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

Specify this id in each reRender of action

Like

reRender="pageMsg"
0
1

I was unable to get the set of circumstances you posted from your question to replicate. When I set up a static method, and access it from the current page context, the messages are added to the page after the rerender.

I used the following two classes, and the below page to test this.

<apex:page controller="TestPageController" >
    <apex:form id="someForm"> 
        <apex:pageMessages id="someMessages" />
        <apex:pageblock title="Detail" mode="mainDetail" >
            <apex:commandButton value="Test" action="{!Test}" reRender="someForm,someMessages" />
        </apex:pageblock> 
    </apex:form> 
</apex:page>

Heres the page controller:

public with sharing class TestPageController {

    public TestPageController() {}

    public PageReference Test() {
        StaticHelperClass.AddPageMessage();

        return null;
    }
}

And heres the static class I used.

public with sharing class StaticHelperClass {

    public static void AddPageMessage() {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, 'Trying to see if static classes can access current page context.'));
    }

}

When I press the button on the page, I get a successful rerender, and an error message on the screen.

Error Message

I tried to replicate your code as best as I could from the edits you posted to your question, but was clearly unable to replicate the issue. I'd recommend reducing your code down to a very minimal example to reproduce the behavior you are experiencing and editing your post feature that example.

4
  • This is NOT correct: "If its not returning a PageReference, it won't rerender the page".
    – Eric
    Commented Jun 29, 2016 at 21:16
  • 1
    I was definitely taught that early on, though I'm not sure I ever saw any supporting documentation to prove or disprove.
    – Adrian Larson
    Commented Jun 29, 2016 at 23:07
  • @AdrianLarson - I just tried it with a basic class, button, and outputpanel with rendered set to a property initialized as a false value. Method in class was returning void. Method set flag to true. On ballon click page refreshed and output panel was shown. I always was curious why all documentation should page reference return type and always returned null. I guess it used to be the case but no longer is in my test unless I am misunderstanding the requirement. I would post the code for the test but it is not appropriate as an answer here
    – Eric
    Commented Jun 30, 2016 at 5:39
  • I was entirely unaware that you could cause a rerender without returning a PageReference, and will likely ask a question sometime soon regarding conditions of rerenders, but in the meantime, Ill edit that line out of the question. Commented Jun 30, 2016 at 14:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .