Here I’ve a custom “Student__c” object. There is a validation rule on “Date_of_Birth__c” field, that Date of Birth cannot be greater than today. And I’m using a visualforce page to insert the data in “Student__c” object. So, I need to show the validation rule error message in visualforce page.
Validation Rule:
Below is my controller which gave the solution for displaying validation error message on visualforce page.
Controller:
public with sharing class StudentExt {
public Student__c student{get;set;}
public StudentExt(ApexPages.StandardController controller) {
student = (Student__c)controller.getRecord();
}
public Pagereference saveStudent() {
try {
Upsert student;
return new Pagereference('/' + student.Id);
}
catch(DMLException de) {
Apexpages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.FATAL, de.getDmlMessage(0)));
return NULL;
}
catch(Exception e) {
Apexpages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.FATAL, e.getMessage()));
return NULL;
}
}
}
Here in below example, I’ve a requirement on selection of “Category” picklist value “Product”, “Detail” inputfield will enable with required mark else will disable.
Action Region will wrap only the input Field and not the complete pageBlockSectionItem. Place pageBlockSectionItem tags inside a pageBlockSection, which in turn should be in a pageBlock. This will provide the correct formatting.