---
schema_version: "1"
id: 35855d735db6a4d4d9673798c064945f1a42da90b52d33b17c9fd70dcc6e6818
title: Validation Rules and Custom Controllers
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/salesforce-pages-developers-guide/guides/pages/controller/pages-controller-validation.md
canonical_url: https://developer.salesforce.com/docs/platform/salesforce-pages-developers-guide/guide/pages-controller-validation.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: acf821c4b8c0dccf64b6cbda26f4a88e3b286bb84554c26e5866f7709662d1f9
products:
  - Visualforce Developer Guide
  - Platform Developer Content
---

# 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:

<!-- otherprops: apex_page set_clipboard -->
```markup
<apex:page controller="MyController" tabStyle="Account">
  <apex:messages/>
  <apex:form>
   <apex:pageBlock title="Hello {!$User.FirstName}!">
     This is your new page for the {!name} controller. <br/>
     You are viewing the {!account.name} account.<br/><br/>
     Change Account Name: <p></p>
     <apex:inputField value="{!account.name}"/> <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"/>
   </apex:pageBlock>
  </apex:form>
</apex:page>
```

**note:** The ID of a valid account record must be specified as a query parameter in the URL for this page to render. For example, http://_MyDomainName_login_URL_/apex/myValidationPage?id=001x000xxx3Jsxb.

You need to write a custom controller like the following:

<!-- otherprops: apex_code set_clipboard -->
```controller
public class MyController {
  Account account;

  public PageReference save() {
    try{
        update account;
       }
    catch(DmlException ex){
        ApexPages.addMessages(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.

