Validator to validate To and From date in ADF

Use Case – Create a custom validator to validate and To/From Date

Solution – 


<?xml version=’1.0′ encoding=’UTF-8′?>
<jsp:root xmlns:jsp=”; version=”2.1″
< contentType=”text/html;charset=UTF-8″/>
<af:document id=”d1″>
<af:form id=”f1″>
<af:panelFormLayout id=”pfl1″>
<af:inputDate label=”End Date” id=”endDate”>
<f:validator validatorId=”DateToFromValidator”/>
<f:attribute name=”startDate” value=”startDate”/>
<af:commandButton text=”Submit” id=”cb1″/>

Validator – 

package view;

import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;

import javax.faces.validator.Validator;

import javax.faces.validator.ValidatorException;


public class DateToFromValidator implements Validator{
public void validate(FacesContext facesContext, UIComponent uIComponent,
Object object) throws ValidatorException {
String dueDateComponentId = (String)uIComponent.getAttributes().get(“startDate”);
RichInputDate richInputDate =(RichInputDate) uIComponent.findComponent(dueDateComponentId);
Date startDate = (Date)richInputDate.getValue();
Date endDate = (Date)object;
String labelStartDate = richInputDate.getLabel();
String labelEndDate = ((RichInputDate)uIComponent).getLabel();
if((startDate == null)||(endDate == null)||(endDate.compareTo(startDate)<0)){
FacesMessage facesMessage = new FacesMessage();
facesMessage.setSummary(labelStartDate + “/”+ labelEndDate + ” Validation Error!”);
facesMessage.setDetail(labelStartDate + ” should be less than ” + labelEndDate);
facesContext.addMessage(uIComponent.getId(), facesMessage);

Config File (Adfc-Config)- 

<?xml version=”1.0″ encoding=”windows-1252″?>
<faces-config version=”1.2″ xmlns=””&gt;


You are done enjoy!!!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s