Is there a way to re-use the validation rules applied to some fields in Salesforce to a Visualforce page automaticaly?

I have a CustomController right now supporting the saving process of a field, but some required fields in the default object are not required in the Visualforce (unless I set them manually).


  • Are you talking about Validation rules or marking a field required on the Page Layout?
    – Mike Chale
    Nov 26, 2012 at 19:04
  • @MikeChale I'm talking about the various rules, such as required, but also validation rules for fields (such as a validation for a phone number, to be a proper format and all).
    – jpmonette
    Nov 26, 2012 at 19:05
  • Validation rules only execute on a database commit, you cannot invoke them explicitly from a VF page. Nov 26, 2012 at 21:45

3 Answers 3


Validation Rules only execute when a database commit occurs (update/ insert/ upsert)

You cannot explicitly reference Validation Rules in Visualforce.

However you can logic you've already written in Validation Rules by including the apex:pageMessages tag in your VisualForce page, and letting the validation rules kick in on save - if any failures occur, they will be displayed on the VisualForce page.

  • 1
    If you use the addError method to highlight an error on specific field, visualforce will also highlight this field in error providing you have used the apex:detail or apex:inputField components. Nov 26, 2012 at 22:10
  • I already have the apex:pageMessages on the page, but I don't know how to return errors when I receive some. When I insert data with my custom controller or update, it is always bypassing every validation rules.
    – jpmonette
    Nov 28, 2012 at 16:49
  • You dont have to do anything explicitly, unless you are catching those exceptions in your controller and suppressing the exception. Use .addError to add the exception, if your handling it in your controller. Nov 28, 2012 at 16:51
  • Well, let's take an example. I have a VisualForce page using a standardController. When I click on {!save} (even with the <apex:pageMessages/> on), I don't receive any error. Every output is sent back without a problem.
    – jpmonette
    Nov 28, 2012 at 16:55
  • How about when you save the record via a standard page? do you get the validation error message. Nov 28, 2012 at 17:07

Well its true that validation rule fires only when you do a DML. But You can always take advantage of them in a VF page. So few changes that I will suggest use <apex:inputField> instead of <apex:inputText> and while saving or executing the save from a command button try to rerender the component where all the reside. All the system validation will fire and the results will be displayed beside the field or on top of the page.

Ohh and dont forget to <apex:pageMessages>

And remember when you use a <apex:inputField> field all the FLS will be automatically applied to the field according to your/user profile.


Validation rules always run if they are active. If a field is defined as required then it will be required on all Page Layouts and enforced at the system level as well. Fields marked required on a Layout-by-Layout basis will need to be marked required on your Visualforce pages as well.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .