Documentation Version
Summer '15 (API version 34.0)
  • Winter '16 Preview (API version 35.0) 35.0
  • Summer '15 (API version 34.0) 34.0
  • Spring '15 (API version 33.0) 33.0
  • Winter '15 (API version 32.0) 32.0
  • Summer '14 (API version 31.0) 31.0
  • Spring '14 (API version 30.0) 30.0
  • Winter '14 (API version 29.0) 29.0
  • English
  • Japanese

Validation Rules and Custom Controllers

If a user enters data on a Visualforce page that uses a custom controller, and that data causes a validation rule error, the error can be displayed on the Visualforce page. Like a page that uses a standard controller, 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:messages> component within the <apex:page> to display the error. However, to get the information to the page, the custom controller must catch the exception.

For example, suppose you have the following page:
<apex:page controller="MyController" tabStyle="Account">
   <apex:pageBlock title="Hello {!$User.FirstName}!">
     This is your new page for the {!name} controller. <br/>
     You are viewing the {!} account.<br/><br/>
     Change Account Name: <p></p>
     <apex:inputField value="{!}"/> <p></p>
     Change Number of Locations:
     <apex:inputField value="{!account.NumberofLocations__c}" id="Custom_validation"/> 
         <p>(Try entering a non-numeric character here, then hit save.)</p><br/><br/>
     <apex:commandButton action="{!save}" value="Save New Account Name"/>


The ID of a valid account record must be specified as a query parameter in the URL for this page to render. For example,

You need to write a custom controller like the following:
public class MyController {
  Account account;

  public PageReference save() {
        update account;
    catch(DmlException ex){
    return null;

  public String getName() {
    return 'MyController';

  public Account getAccount() {
    if(account == null)
      account = [select id, name, numberoflocations__c from Account
        where id = :ApexPages.currentPage().getParameters().get('id')];
      return account;

When the user saves the page, if a validation error is triggered, the exception is caught and displayed on the page as they are for a standard controller.