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
marcusaureliusmarcusaurelius 

Validation Rules & VF Pages

I'm trying to render validation rule error messages on my visual force pages.  Does anyone know how this can easily be done? 

 

From what I'm reading, I need to recreate the validation rules in an extension? (is that true!!?) 

 

Hoping for an easier approach!

 

TIA

Best Answer chosen by Admin (Salesforce Developers) 
CaptainObviousCaptainObvious

Use the apex pageMessages tag to render validation rule errors.
For Example:

<apex:page standardController="My_Object__c" tabStyle="My_Object__c" extensions="Custom_Ext"> <apex:outputPanel id="thePanel"> <apex:form id="myForm"> <apex:pageBlock title="PageBlock Title" mode="edit" id="pgB"> <apex:pageMessages id="pageErrors"></apex:pageMessages> <apex:pageBlockButtons location="top" id="pgTButtons" > <apex:commandButton value="Save" action="{!save}" rerender="thePanel" /> </apex:pageblockButtons> ... ... ... </apex:pageBlock> </apex:form> </apex:outputPanel> </apex:page>

In addition, you may have to set required="true" in your input fields and enclose the form in an outputpanel as shown.

All Answers

CaptainObviousCaptainObvious

Use the apex pageMessages tag to render validation rule errors.
For Example:

<apex:page standardController="My_Object__c" tabStyle="My_Object__c" extensions="Custom_Ext"> <apex:outputPanel id="thePanel"> <apex:form id="myForm"> <apex:pageBlock title="PageBlock Title" mode="edit" id="pgB"> <apex:pageMessages id="pageErrors"></apex:pageMessages> <apex:pageBlockButtons location="top" id="pgTButtons" > <apex:commandButton value="Save" action="{!save}" rerender="thePanel" /> </apex:pageblockButtons> ... ... ... </apex:pageBlock> </apex:form> </apex:outputPanel> </apex:page>

In addition, you may have to set required="true" in your input fields and enclose the form in an outputpanel as shown.

This was selected as the best answer
marcusaureliusmarcusaurelius
awesome! tx much.
JusticePsychJusticePsych

I'm having a related issue and for some reason, it seems that the pageMessages function isn't working.

 

I have a Visualforce page that is being controlled by a custom object.  There are validation rules set up for the custom object from within the Customize menu in Salesforce.  The criteria are:

 

1) If FieldX = blank && (checkbox = True)

 

It is supposed to simply produce an error message at the top of the screen, but instead it redirects me to an Visualforce Error page with an ugly exception error followed by the proper text of the validation rule "You must enter a value for FieldX"

 

Unfortuantely, the user then has to navigate back to the edit page, where they find that all their data has been erased and they must start all over again.

CaptainObviousCaptainObvious

If you're using a custom controller, look for any try/catch blocks in the controller and make sure to add ApexPages.addMessages().

For example:

public PageReference save(){ try { insert myCustomObj; return new PageReference('/a0o/o'); } catch (DmlException e) { ApexPages.addMessages(e); return null; } }

 

Message Edited by CaptainObvious on 01-11-2010 12:40 PM
rameshvpsgrameshvpsg

I have a VF page to enter monthly revenue along with comments and added validation rule to make comment mandatory if revenue is modified. How to populate the validation rule error message in my VF. Below is my VF page. Please suggest.

<apex:page standardController="Price_Book__c"  tabStyle="Price_Book__c"

sidebar="false" extensions="MultipelRevenueEditFee" >

 

<apex:panelGroup rendered="{!Price_Book__c.Checkbox__c==false}" >

 

<apex:form >

<apex:pageBlock >

<apex:pageMessages />

<apex:pageBlockButtons >

    <apex:commandButton value="Save & Return to Forecast" action="{!save}"/>   

    <apex:commandButton value="Save & Return to Opportunity" action="{!return1}"/>

    <apex:commandButton value="Cancel" action="{!cancel}"/>

    </apex:pageBlockButtons>

<table width="100%">

<tr>

    <th width="1%" align="left"></th>

    <th width="15%" align="left"><h1 style="font-size:12pt">FINANCE</h1></th>

</tr>

</table>

 

<hr/>

<table width="100%">

<tr>

    <td width="8%"><b>Service Family</b></td>

    <td width="8%"><b>Record Type</b></td>

    <td width="8%"><b>App Name</b></td>   

    <td width="10%"><b>Date</b></td>  

    <td width="8%"><b>Forecast</b></td>

    <td width="20%"><b>Comments</b></td>

   

</tr>

<tr><td><br/></td></tr>

<apex:repeat value="{!rev}" var="revenue">

<apex:repeat value="{!price}" var="p">

<tr>

    <td width="8%"><apex:outputField value="{!p.FTE_Based_Services__c}"/></td>

    <td width="8%"><apex:outputField value="{!p.RecordTypeId}"/></td>

    <td width="8%"><apex:outputField value="{!p.Related_FTE_Service__c}"/></td> 

    <td width="10%"><apex:inputField value="{!revenue.Date__c}"/></td>    

    <td width="8%"><apex:inputField value="{!revenue.Total_Value_by_Forecast__c}" style="width:90pt"/></td>

    <td width="20%"><apex:inputField value="{!revenue.Comments__c}"/></td>

   

</tr>

</apex:repeat>

</apex:repeat>

</table>

</apex:pageBlock>

</apex:form>

</apex:panelGroup>

 

</apex:page>

Saleforce's manSaleforce's man

Thanks so much. That is my need