1. I am displaying 5 types of error message her.
2. Display message by creating new instance of ApexPages.message and then adding message to Apexpages using
ApexPages.addmessage. Then displaying these messages in visualforce page..
In the example below, we are showing 5 input fields of account. I have added a button on visualforce page. Different type of message will be shown on visualforce page if we will keep any field blank.
VISUALFORCE PAGE:
<apex:page standardController="Account"
extensions="ShowErrorMessage" showHeader="false"
sidebar="false">
<apex:form >
<apex:pageblock >
<apex:pageMessages
id="showmsg"></apex:pageMessages>
<apex:panelGrid columns="2">
Account Name:
<apex:inputText value="{!account.name}"/>
Account Number:
<apex:inputText value="{!account.AccountNumber}"/>
Account Phone:
<apex:inputText value="{!account.phone}"/>
Account Site: <apex:inputText
value="{!account.site}"/>
Account Industry:
<apex:inputText value="{!account.industry}"/>
<apex:commandButton
value="Click" action="{!save}" rerender="showmsg"/>
</apex:panelGrid>
</apex:pageblock>
</apex:form>
</apex:page>
APEX CLASS:
public class
ShowErrorMessage{
public Account account{get;set;}
public
ShowErrorMessage(ApexPages.StandardController controller) {
account = new Account();
}
public void save()
{
if(account.name == '' || account.name == null)
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Account name'));
if(account.AccountNumber == '' ||
account.AccountNumber == null)
ApexPages.addmessage(new
ApexPages.message(ApexPages.severity.ERROR,'Please enter Account number'));
if(account.phone == '' || account.phone ==
null)
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Account phone
number'));
if(account.site == '' || account.site == null)
ApexPages.addmessage(new
ApexPages.message(ApexPages.severity.INFO,'Please enter Account site'));
if(account.industry == '' || account.industry
== null)
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Please enter Account industry detail.'));
}
}
0 comments:
Post a Comment
If you have any doubts, please let me know.