Understanding Component in Visualforce

The <apex:pageMessages> component is used to display a collection of messages on a Visualforce page. It is typically used in scenarios where you need to show multiple messages to the user, such as a list of validation errors or multiple success notifications.


Syntax

xmlCopyEdit<apex:pageMessages />

Key Attributes

AttributeDescription
renderedOptional. Specifies whether the component is displayed or not. Defaults to true.
escapeOptional. Whether to escape HTML in messages. Defaults to true.
showDetailOptional. If true, shows the message details. If false, shows only the summary.

Example 1: Basic Usage

xmlCopyEdit<apex:page>
    <apex:pageMessages />
</apex:page>
  • This will display all the page messages (info, error, warning, success) generated by the page.

Example 2: Displaying Messages with showDetail="false"

xmlCopyEdit<apex:page>
    <apex:pageMessages showDetail="false" />
</apex:page>
  • This will display only the summary text of the messages, without showing any additional details.

Example 3: Displaying Multiple Messages

xmlCopyEdit<apex:page>
    <apex:pageMessage summary="Warning! Please check your input." severity="warning" />
    <apex:pageMessage summary="Success! Your data is saved." severity="success" />
    <apex:pageMessages />
</apex:page>
  • Multiple messages (info, success, warning, etc.) will be displayed on the page.

When to Use <apex:pageMessages>

  • When you need to show multiple messages (error, success, etc.) at once.
  • Useful for displaying validation errors or results of bulk operations.

Best Practices

  • Use <apex:pageMessages> when you want to display a list of messages that have been added to the page context.
  • Avoid cluttering the page with too many messages; group them logically for better user experience.
  • Be mindful of message severity and try not to overwhelm the user with too many warnings or errors at once.