I have used a custom object for validation. You need to do the changes based on the object and Field API Name.
Setp 1: Create a Custom Object: Stationary
Step 2: Create a Apex Class
Navigation: Setup => Develop => Apex Classes
Class Name: CustomStationaryController
public class CustomStationaryController {
public stationary__c stat{get;set;}
public boolean isEditPage{get;set;}
public CustomStationaryController(){
stat = new stationary__c();
isEditPage = true;
}
public PageReference EnableEdit(){
isEditPage = true;
return null;
}
public pageReference SaveStationary(){
if(validateStationaryValuesCombinedMSG()){
if(stat.id == null)
insert stat;
else
update stat;
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Stationary item created succesfully.'));
isEditPage = false;
}
return null;
}
private boolean validateStationaryValues(){
boolean isValidationPass = true;
//To validate Name Field
if(String.isBlank(stat.name)){
isValidationPass = false;
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Enter the Stationary Name.'));
}
//To validate Item Field
if(String.isBlank(stat.Item__c)){
isValidationPass = false;
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Select the Stationary Item.'));
}
//To validate Quantity Field
if(stat.Quantity__c == null){
isValidationPass = false;
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Enter the Stationary Quantity.'));
}
//To validate List Price Field
if(stat.List_Price__c == null){
isValidationPass = false;
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Enter the Stationary item List Price.'));
}
//To validate Selling Price Field
if(stat.Selling_Price__c == null){
isValidationPass = false;
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Enter the Stationary item Selling Price.'));
}
//To validate Discount Price Field
if(stat.Discount_Price__c == null){
isValidationPass = false;
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Enter the Stationary item Discount.'));
}
return isValidationPass;
}
private boolean validateStationaryValuesCombinedMSG(){
boolean isValidationPass = true;
//To validate Name Field
if(String.isBlank(stat.name) || String.isBlank(stat.Item__c) || stat.Quantity__c == null || stat.List_Price__c == null || stat.Selling_Price__c == null || stat.Discount_Price__c == null){
isValidationPass = false;
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please enter the value, These fields are required(Stationary Name, Item Name, Quantity, List Price, Selling Price and Discount Price).'));
}
return isValidationPass;
}
}
Step3: Create Visualforce page
Page Name: CustomStationaryPage
<apex:page controller="CustomStationaryController" tabStyle="Stationary__c">
<apex:sectionHeader title="Stationarys" subtitle="Home"/>
<apex:form >
<apex:pageBlock title="Stationary Form" rendered="{!isEditPage}">
<apex:pagemessages ></apex:pagemessages>
<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!SaveStationary}"/>
</apex:pageBlockButtons>
<apex:pageblockSection title="Information" columns="1">
<apex:inputField value="{!stat.name}"/>
<apex:inputField value="{!stat.Item__c}"/>
<apex:inputField value="{!stat.Quantity__c}"/>
<apex:inputField value="{!stat.List_Price__c}"/>
<apex:inputField value="{!stat.Selling_Price__c}"/>
<apex:inputfield value="{!stat.Discount_Price__c }"/>
</apex:pageblockSection>
</apex:pageBlock>
<apex:pageBlock title="Stationary Form" id="detailPage" rendered="{!!isEditPage}">
<apex:pageBlockButtons >
<apex:commandButton value="Edit" action="{!EnableEdit}"/>
</apex:pageBlockButtons>
<apex:pageblockSection title="Information" columns="1">
<apex:outputField value="{!stat.name}"/>
<apex:outputField value="{!stat.Item__c}"/>
<apex:outputField value="{!stat.Quantity__c}"/>
<apex:outputField value="{!stat.List_Price__c}"/>
<apex:outputField value="{!stat.Selling_Price__c}"/>
<apex:outputField value="{!stat.Discount_Price__c }"/>
</apex:pageblockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Output: You can see the Edit page on page load. Once validation pass and record inserted, edit page disable and automatically Stationary detail page will appear with Edit button.
Once Click on Edit button, we navigate to edit page where you can change the value and update the stationary existing record and after save go to detail page.
User will see error message on the page after click on Save button without entering required field's value.
[Edit Page with Error Message]
[Edit Page]
[Detail Page]




No comments:
Post a Comment