Displaying Errors in VisualForce page, using yellow background box. - El Toro - Find articles about Visualforce, Apex, Force.com and Salesforce in general

Print Preview

Displaying Errors in VisualForce page, using yellow background box.

This is easily accomplished by using <apex:pageMessages /> rather than <apex:Messages>.
The data is the same, but <apex:pageMessages /> shows a yellow box with an icon and red titleā€¦. It looks much nicer...

<apex:pageMessages />

String strMsg = 'Show this string';
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, strMsg);

The severity, affects the order in which the messages are shown and the color it uses. For example this code:

ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, '1: ApexPages.Severity.CONFIRM'));
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, '2: ApexPages.Severity.ERROR'));
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.FATAL, '3: ApexPages.Severity.FATAL'));
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, '4: ApexPages.Severity.INFO'));
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING, '5: ApexPages.Severity.WARNING'));

Generates this screen:

If you do not have an Apex controler, you could generate the message in Visualforce only, like this:

<apex:pageMessage title="Visualforce Only"
    summary="You do not need Apex controller"
    detail="This sample shows how you can generate a message in Visualforce without needing Apex. By the way, the strength determines the size of the Icon"
    severity="warning" strength="1" />

comments powered by Disqus

© El Toro . IT @ 2013
Andrés Pérez