Adding error messages to field inputs_final

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 conroller 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 conroller

private ApexPages.Standardconroller stdCtr;

 

// Constructor

public InputFieldErrorExt(ApexPages.Standardconroller std){

stdCtr=std;

}

 

// validates the inputs and adds error messages or delegates to

// the standard conroller 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 it works…

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

 

error

 

The Visualforce page conroller extension declares a Save method, which overrides the  standard conroller’s Save method. If the conact’s Phone and Email fields are both empty, the conroller 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 conroller extension delegates it to the standard conroller 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

0 Comments

Leave a Reply

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Property of Forcetalks.com
Copyright 2017 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?