5

everyone.

I had problem showing errors in my page, and I boiled it down to the use of showHeader="false". Here's my Controller:

public class MyTest1 {
        public MyTest1()
        {
                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'See Error'));
        }

}  

and VF page:

<apex:page controller="MyTest1" showHeader="false" cache="false">
  <h1>Hello</h1>
  <apex:pageMessages rendered="true" />
</apex:page>

The error message will only be displayed if I removed the showHeader="false" I read the documentation and didn't find they are related. I need to customize my page and do not want to use the standard header.

Any suggestion would be appreciated.

3 Answers 3

6

For some reason, messages created inside the constructor are not displayed when the page header isn't shown. If you move that to a method and add it to your actions, it will work. Also, you need to load the standard style sheets since they are usually included with the header. If you don't it looks ugly.

The controller:

<apex:page controller="MyTest1" showHeader="false" standardStylesheets="true" action="{!loadMessages}">
  <h1>Hello</h1>
  <apex:PageMessages />
</apex:page>

The class:

public class MyTest1 {
        public MyTest1() {}

        public void loadMessages() {
             ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'See Error'));
        }
}
0
1

You don't need to call an action method to make your apex:pageMesssages render. What you do need is to wrap your pageMessages in a pageblock, like so:

<apex:pageBlock>
    <apex:pageMessages/>
</apex:pageBlock>

You should now have no problems rendering your messages from the constructor. You will have to style the messages block though!

1
  • 1
    Thanks a lot! This saved me hours of frustrated debugging! Commented Oct 14, 2020 at 10:00
-1

You can try this way and you can add css to apex:pageBlock according to your need.

 <apex:pageBlock>
        <apex:pageMessages rendered="true" /></apex:pageBlock>
 </apex:form>

You must log in to answer this question.

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