If a user enters data on a Visualforce page that uses a standard controller, and that data causes a validation rule error, the error can be displayed on the Visualforcepage. If the validation rule error location is a field associated with an<apex:inputField>component, the error displays there. If the validation rule error location is set to the top of the page, use the<apex:pageMessages>or<apex:messages>component within the<apex:page>to display the error. Message Class When using a standard controller, all validation errors, both custom and standard, that occur when the end user saves the page are automatically added to the page error collections. If there is an inputField component bound to the field with an error, the message is added to the components error collection. All messages are added to the pages error collection. For more information, see Validation Rules and Standard Controllers in the Visualforce Developer's Guide. If your application uses a custom controller or extension, you must use the message class for collecting errors. Instantiation In a custom controller or controller extension, you can instantiate a Message in one of the following ways: • ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity, summary); where ApexPages.severity is the enum that is determines how severe a message is, and summary is the String used to summarize the message. For example: ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL, 'my error msg'); • ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity, summary, detail); where ApexPages. severity is the enum that is determines how severe a message is, summary is the String used to summarize the message, and detail is the String used to provide more detailed information about the error.
ApexPages.Severity Enum Using the ApexPages.Severity enum values, specify the severity of the message. The following are the valid values:
All enums have access to standard methods, such as name and value. ApexPages Methods Use ApexPages to add and check for messages associated with the current page, as well as to reference the current page. In addition, ApexPages is used as a namespace for the PageReference and Message classes. The following table lists the ApexPages methods: sObject Methods sObject methods are all instance methods, that is, they are called by and operate on a particular instance of an sObject, such as an account or contact. The following are the instance methods for sObjects.