Play Games

Search This Blog

Monday, August 8, 2016

How to override standard required field validation messages in visualforce page Salesforce.

Steps :
Step 1: First we need to use required="false" for that field as shown in the below piece of code.
<apex:inputField value="{!acc.name}" required="false"/>
Step 2: To make that field required use the sample code below.
<apex:pageBlockSectionItem > Account Name
   <apex:outputPanel layout="block" style="float: left">
 <apex:outputPanel >
<div class="requiredInput"><div class="requiredBlock"/>
 <apex:inputField value="{!acc.name}" required="false" />          
</div>
 </apex:outputPanel>                              
   </apex:outputPanel>
</apex:pageBlockSectionItem>

Example :
Apex Class : StandardErrorOverrideController
public class StandardErrorOverrideController {
    public account acc {get;set;}
    public StandardErrorOverrideController() {
        acc = new Account();
    }
    public void saveData() {

        try {
        insert acc;
        } catch(DMLException e) {
            if(acc.name =='' || acc.name == null) {
                acc.name.addError('Account Name Cannot be Null');
            }
        }      
   }    
}
Visualforce Page :
<apex:page controller="StandardErrorOverrideController">
<apex:form >
    <apex:pageBlock title="My Content" mode="edit">
        <apex:pageBlockButtons >
            <apex:commandButton action="{!saveData}" value="Save"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection title="My Content Section" columns="2">
             <apex:pageBlockSectionItem > Account Name
               <apex:outputPanel layout="block" style="float: left">
                      <apex:outputPanel >
                            <div class="requiredInput"><div class="requiredBlock"/>
                                  <apex:inputField value="{!acc.name}" required="false" />          
                            </div>
                      </apex:outputPanel>                              
               </apex:outputPanel>
            </apex:pageBlockSectionItem>
            <apex:inputField value="{!acc.site}"/>
            <apex:inputField value="{!acc.type}"/>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>
Output :
With out entering value for account name,click save button.The custom error message will appear as shown in the image below.

Standard Error message will be as shown in the image below.

No comments:

Post a Comment