Adding error messages to field inputs

When users are editing or creating a record via a Visualforce page, they will often make mistakes or enter invalid data. The required fields will present an error message underneath the field itself, but validation rules or exceptions will simply send the user to a new page with a large error message, telling them that the insert or update failed. In this tutorial I will create a Visualforce page to allow a user to create or edit a contact record. The contact standard controller and a controller extension manage the page. The extension controller checks whether the e-mail address or phone number field has been populated. If either of the fields is populated, the record will be saved, but if both are missing, an error message will be added to both fields asking the user to populate at least one of them.

InputFieldErrorExt Extension

/**********************************************
* Extension controller for the "Adding Errors to Input Fields"
* Adds validation errors to sObject fields that can be displayed
* against the field.
***********************************************/

public with sharing class InputFieldErrorExtension {
    // the parent standard controller
    private ApexPages.Standardconroller stdCtr;

    // Constructor
    public InputFieldErrorExt(ApexPages.Standardconroller std) {
        stdCtr=std;
    }

    // validates the inputs and adds error messages or delegates to
    // the standard controller save method
    public PageReference save() {
        PageReference result=null;
        conact con=(conact) stdCtr.getRecord(); 
        if ( (String.IsBlank(con.Email)) && (String.IsBlank(con.Phone)) ) {
            con.email.addError('Please enter an Email address or a phone number');
            con.phone.addError('Please enter a Phone number or  an Email address');
        }
        else {
            result=stdCtr.save();
        }
        return result;
    }
}

InputFieldError Visualforce Page

<apex:page standardconroller="conact" extensions="InputFieldErrorExtension">
    <apex:form>
        <apex:sectionHeader title="Create new conact" />
        <apex:pageBlock>
            <apex:pageBlockSection>
                <apex:inputField value="{!conact.Salutation}"/>
                <apex:inputField value="{!conact.FirstName}"/>
                <apex:pageBlockSectionItem />
                <apex:inputField value="{!conact.LastName}"/>
                <apex:inputField value="{!conact.Phone}"/>
                <apex:inputField value="{!conact.Email}"/>
            </apex:pageBlockSection>
            <apex:pageBlockButtons>
                <apex:commandButton value="Save" action="{!save}" />
                <apex:commandButton value="Cancel" action="{!cancel}" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

How does it work?

Leaving both the Phone and Email fields blank causes an error message to be displayed underneath the fields.

error

The Visualforce Page Controller Extension declares a Save method, which overrides the  standard controller's Save method. If the contact's Phone and Email fields are both empty, the controller uses the addError method of the sObject field to associate an error message with the field. When the page is rendered, Visualforce will automatically display the error message with the field. If either of the Phone or Email fields are populated, the controller extension delegates it to the standard controller Save method and returns the result.

public PageReference save() {
    PageReference result=null;
    conact con=(conact) stdCtr.getRecord();
    if((String.IsBlank(con.Email)) && (String.IsBlank(con.Phone))) {
        con.email.addError('Please enter an email address or phone number');
        con.phone.addError('Please enter a phone number or email address');
    }
    else {
        result=stdCtr.save();
    }
    return result;
}

Page Reference: http://blog.smartbear.com/wp-content/uploads/2014/05/fault-injection-software-testing.jpg

Popular Salesforce Blogs