4

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).

Thanks.

3
  • Are you talking about Validation rules or marking a field required on the Page Layout? – Mike Chale Nov 26 '12 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 '12 at 19:05
  • Validation rules only execute on a database commit, you cannot invoke them explicitly from a VF page. – techtrekker Nov 26 '12 at 21:45
4

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.

6
  • 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. – Andrew Fawcett Nov 26 '12 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 '12 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. – techtrekker Nov 28 '12 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 '12 at 16:55
  • How about when you save the record via a standard page? do you get the validation error message. – techtrekker Nov 28 '12 at 17:07
2

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.