function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
paulo.orquillopaulo.orquillo 

Validation Rules in Visualforce Pages

Hi newbie here and have already read alot and researched on the topic before posting but need more help for straight forward response.

 

I've created a wizard for a custom object for creating new records.  If I dont use the wizard and use the platform I can create a new record with all the validations I made working- they appear on the field being validated. But when I create my wizard I'm not getting any validation errors next to the field, it only displays the error in field for required.

 

eg. I have a validation rule that checks a birthdate to make sure user is above 15 years old.

 

If I try to save my record I then get an error.

 

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, You must be 15 yrs old or older.: [Birth_Date__c]

 

I appreciate any tips and suggestions for the proper approach to take.

MikeGinouMikeGinou

My best guess is that you aren't seeing field errors because the final page of your wizard does not contain an apex:inputField bound to the age field. 

 

Perhaps if you could simplify your code down to the essence and provide some of it, we might be able to spot the trouble better, though.

 

-Mike

paulo.orquillopaulo.orquillo

Thanks for the quick reply.

 

I made the custom object as a standardController and created a new class (newHire) as an extension for the wizard functionality of next, cancel and save.

 

The inputField is bound to the Birth_Date__c custom field. 

 

<apex:inputField value="{!Employees__c.Birth_Date__c}"/>

 

Here is the trimmed down first page of the wizard.

 

<apex:page standardController="Employees__c" extensions="newHire">
  <script>
  function confirmCancel() {
      var isCancel = confirm("Are you sure you wish to cancel?");
      if (isCancel) return true;
 
     return false;
  } 
  </script>
  <apex:sectionHeader title="Hire New Employee Wizard" />
    <apex:form >
      <apex:pageBlock title="Step 1" mode="edit">
    <apex:Pagemessages ></apex:Pagemessages>
        <apex:pageBlockButtons >
          <apex:commandButton action="{!step2}" value="Next"/>
          <apex:commandButton action="{!cancel}" value="Cancel"
                              onclick="return confirmCancel()" immediate="true"/>
        </apex:pageBlockButtons>
   

        <apex:pageBlockSection title="Define Employee Basic Information">
       
        </apex:pageBlockSection>
   

<apex:pageBlockSection title="Photo" columns="2">
        <apex:inputField value="{!Employees__c.Photo__c}"/><br/>
</apex:pageBlockSection>
        <apex:pageBlockSection title="Basic Information" columns="2">

        <apex:inputField value="{!Employees__c.Employer__c}"/>
        <apex:inputField value="{!Employees__c.Employee_ID__c}"/>
        <apex:inputField value="{!Employees__c.First_Name__c}"/>

        <apex:inputField value="{!Employees__c.Middle_Name__c}"/>

        <apex:inputField value="{!Employees__c.Last_Name__c}"/>

        <apex:inputField value="{!Employees__c.Gender__c}" required="true"/>
        <apex:inputField value="{!Employees__c.Birth_Date__c}"/>

       
       </apex:pageBlock>
    </apex:form>
</apex:page>

 

If I do this on normal platform and not the visualforce wizard page for adding new records. The validation rule works and displays the error next to the field.

 

This is my validation rule.

 

Formula: FLOOR((TODAY()- Birth_Date__c )/365.2425) < 15

Error Message: You must be 15 yrs old or older.

 

Appreciate any hint to move forward.

 

MikeGinouMikeGinou

The page does not invoke a save (as it is the first page in a wizard), therefore the validation rule does not run. The validation rule won't run until you get to the last page of the wizard, on which you'll have a button bound to (presumably) the standard controller.

 

Since the last page, the one with the save button, doesn't have the inputfield which is bound to Birth_Date__c, you don't get the automatic validation error on the page.

 

One possible option to use, would be to have each page of the wizard attempt to save the record. This may, or may not, work. It depends on the way the object is defined and how the wizard pages are arranged (e.g. If you have a lot of triggers doing complex work, you might prefer not to repeatedly invoke the triggers; or if you have vital, required fields on the last page of the wizard, this approach might be difficult).