The <apex: message> is another important component for displaying a message(error or warning) for a specific component on the VF page. If we do not specify the <apex: message> component on the VF page, all the error and warning messages will be visible only in debug logs, not in UI.
The <apex: message> 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: message dir="RTL"></apex: message>
2. for
The for is a string-type attribute used for specifying the Id of the component with which the message should be associated.
Syntax:
<apex: message for="cmpId"></apex: message>
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: message id="messageId"></apex: message>
4. lang
The lang is a string-type attribute used for specifying the base language for the generated HTML output. The "en" and "en-US" are the two possible values for this attribute.
Syntax:
<apex: message lang="en-US"></apex: message>
5. rendered
The rendered is a Boolean-type attribute used to specify 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: message rendered="false"></apex: message>
6. style
The style is a string-type attribute used for specifying the inline CSS applied on this component to display the message.
Syntax:
<apex: message style="color:green;"></apex: message>
7. styleClass
The styleClass is a string-type attribute used to specify the CSS stylesheet applied on this component to display the message.
AD
Syntax:
<apex: message styleClass="className"></apex: message>
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: message title="Tooltip Text"></apex: message>
Let's take an example to understand how we can use this component on the VF page:
ApexMessageExample.vfp
<!-- apex page to elaborate the use of apex message component-->
<apex:page controller="ApexMessageExampleController" tabStyle="Account">
<!-- user style tag to override the standard style-->
<style>
.locError {
color: blue;
font-weight: strong;
}
.empError {
color: green;
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:message for="Location_validation" styleClass="locError" /> <p/>
<apex:message for="Employee_validation" styleClass="empError"/> <p/>
</apex:pageBlock>
</apex: form>
</apex: page>
ApexMessageExampleController.apxc
public class ApexMessageExampleController {
// create an instance of Account
public Account acc{get;set;}
public string recId;
//default constructor
public ApexMessageExampleController(){
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 'ApexMessageExampleController';
}
}