Newer Version Available
apex:message
A message for a specific component, such as a warning or error. If an <apex:message> or <apex:messages> component is not included in a page, most warning and error messages are only shown in the debug log.
Example
1<!-- For this example to render properly, you must associate the Visualforce page
2with a valid account record in the URL.
3For example, if 001D000000IRt53 is the account ID, the resulting URL should be:
4https://MyDomain_login_URL/apex/myPage?id=001D000000IRt53
5See the Visualforce Developer's Guide Quick Start Tutorial for more information. -->
6
7<!-- Page: -->
8<apex:page controller="MyController" tabStyle="Account">
9 <style>
10 .locationError { color: blue; font-weight: strong;}
11 .employeeError { color: green; font-weight: strong;}
12 </style>
13
14 <apex:form >
15 <apex:pageBlock title="Hello {!$User.FirstName}!">
16 This is your new page for the {!name} controller. <br/>
17 You are viewing the {!account.name} account.
18
19 <p>Number of Locations: <apex:inputField value="{!account.NumberofLocations__c}"
20 id="Location_validation"/>
21 (Enter an alphabetic character here, then click Save to see what happens.) </p>
22
23 <p>Number of Employees: <apex:inputField value="{!account.NumberOfEmployees}"
24 id="Employee_validation"/>
25 (Enter an alphabetic character here, then click Save to see what happens.) </p>
26 <p />
27 <apex:commandButton action="{!save}" value="Save"/>
28 <p />
29 <apex:message for="Location_validation" styleClass="locationError" /> <p />
30 <apex:message for="Employee_validation" styleClass="employeeError" /> <p />
31 </apex:pageBlock>
32 </apex:form>
33</apex:page>
34
35/*** Controller ***/
36public class MyController {
37 Account account;
38
39 public PageReference save() {
40 try{
41 update account;
42 }
43 catch(DmlException ex){
44 ApexPages.addMessages(ex);
45 }
46 return null;
47 }
48
49 public String getName() {
50 return 'MyController';
51 }
52
53 public Account getAccount() {
54 if(account == null)
55 account = [select id, name, numberofemployees, numberoflocations__c from Account
56 where id = :ApexPages.currentPage().getParameters().get('id')];
57 return account;
58 }
59}Attributes
| Attribute Name | Attribute Type | Description | Required? | API Version | Access |
|---|---|---|---|---|---|
| dir | String | The direction in which the generated HTML component should be read. Possible values include "RTL" (right to left) or "LTR" (left to right). | 10.0 | global | |
| for | String | The ID of the component with which the message should be associated. | 10.0 | global | |
| id | String | An identifier that allows the message component to be referenced by other components in the page. | 10.0 | global | |
| lang | String | The base language for the generated HTML output, for example, "en" or "en-US". For more information on this attribute, see the W3C specifications. | 10.0 | global | |
| rendered | Boolean | A Boolean value that specifies whether the component is rendered on the page. If not specified, this value defaults to true. | 10.0 | global | |
| style | String | The style used to display the message, used primarily for adding inline CSS styles. | 10.0 | global | |
| styleClass | String | The style class used to display the message, used primarily to designate which CSS styles are applied when using an external CSS stylesheet. | 10.0 | global | |
| title | String | The text to display as a tooltip when the user's mouse pointer hovers over this component. | 10.0 | global |