The <apex: messages> is another important component for displaying all the messages(error or warning) generated for all the components on the VF page. If we do not specify the <apex: messages> component on the VF page, all the error and warning messages will be visible only in debug logs, not in UI(VF Page).
The <apex: messages> component has the following attributes on VF Page:
1. dir
The dir is a string-type attribute used to specify the direction in which the generated HTML component should be read. RTL and LTR are the two possible values for this attribute.
Syntax:
<apex: messages dir="RTL"></apex: messages>
2. globalOnly
The globalObly is a Boolean-type attribute that specifies whether only messages that are not associated with any client ID are displayed. The boolean value false is set as a default value for this attribute.
Syntax:
<apex: messages globalOnly="true"></apex: messages >
3. id
The id is a string-type attribute, i.e., a unique identifier that allows this component to be referenced by other components on the page.
Syntax:
<apex: messages id="messageId"></apex: messages>
4. lang
The lang is a string-type attribute used to specify the base language for the generated HTML output. The "en" and "en-US" are the two possible values for this attribute.
Syntax:
<apex: messages lang="en-US"></apex: messages>
5. rendered
The rendered is a Boolean-type attribute that specifies whether this component should be rendered on the page. The boolean value true is set as a default value for this attribute.
Syntax:
AD
<apex: messages rendered="false"></apex: messages>
6. style
The style is a string-type attribute used to specify the inline CSS applied to this component to display the message.
Syntax:
<apex: messages style="color:green;"></apex: messages>
7. styleClass
The styleClass is a string-type attribute used to specify the CSS stylesheet applied to this component to display the message.
AD
Syntax:
<apex: messages styleClass="className"></apex: messages>
8. title
The title is a string-type attribute used to specify the text displayed when the mouse pointer hovers over this component.
Syntax:
<apex: messages title="Tooltip Text"></apex: messages>
9. layout
The layout is a string-type attribute used to specify the layout type used to display the error messages. The "list" or "table" are the two possible values for this attribute. The "list" is set as a default value for this attribute.
Syntax:
<apex: messages layout="table"></apex: messages>
Let's take an example to understand how we can use this component on the VF page:
ApexMessagesExample.vfp
<!-- apex page to elaborate the use of apex messages component-->
<apex:page controller="ApexMessagesExampleController" tabStyle="Account">
<!-- user style tag to override the standard style-->
<style>
.errorMessages {
color: blue;
font-weight: strong;
}
</style>
<!-- use apex form to display account information-->
<apex: form >
<apex:pageBlock title="Hello {!$User.FirstName+' '+$User.LastName}!">
New page for the {!name}. <br/>
Showing information of {!acc.name} Account.
<br/>
<b>Note: </b>Enter an alphabetic character, then click Save to see what happens.
<p>
Number of Locations:
<apex:inputField value="{!acc.NumberofLocations__c}" id="Location_validation"/>
</p>
<p>Number of Employees: <apex:inputField value="{!acc.NumberOfEmployees}" id="Employee_validation"/></p>
<p/>
<apex:commandButton action="{!save}" value="Save"/>
<p/>
<apex:messages styleClass="errorMessages"/> <p/>
</apex:pageBlock>
</apex: form>
</apex: page>
ApexMessagesExampleController.apxc
public class ApexMessagesExampleController {
// create an instance of Account
public Account acc{get;set;}
public string recId;
//default constructor
public ApexMessagesExampleController(){
recId = ApexPages.currentPage().getParameters().get('id');
acc = [select id, name, numberofemployees, numberoflocations__c from Account where id = :recId];
}
// create save() method
public PageReference save() {
try{
update acc;
}
catch(DmlException ex){
ApexPages.addMessages(ex);
}
return null;
}
// getter to get the name of the controller
public String getName() {
return 'ApexMessagesExampleController';
}
}