Validate Start to End date in ADF

Posted: August 28, 2014 in ADF Components, Calendar, Step-By-Step
Tags: , ,

Oracle ADF

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

Solution – 

JSPX – 

<?xml version=’1.0′ encoding=’UTF-8′?>
<jsp:root xmlns:jsp=”http://java.sun.com/JSP/Page” version=”2.1″
xmlns:f=”http://java.sun.com/jsf/core”
xmlns:h=”http://java.sun.com/jsf/html”
xmlns:af=”http://xmlns.oracle.com/adf/faces/rich”>
<jsp:directive.page contentType=”text/html;charset=UTF-8″/>
<f:view>
<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:inputDate>
<af:commandButton text=”Submit” id=”cb1″/>
</af:panelFormLayout>
</af:form>
</af:document>
</f:view>
</jsp:root>

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;

import oracle.adf.view.rich.component.rich.input.RichInputDate;

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);
facesMessage.setSeverity(facesMessage.SEVERITY_ERROR);
facesContext.addMessage(uIComponent.getId(), facesMessage);
}
}
}

Config File (Adfc-Config)- 

<?xml version=”1.0″ encoding=”windows-1252″?>

View original post 16 more words

Comments
  1. Juliana Spencer says:

    please explain this.. im new in ADF.. 😦

    • adfgok says:

      You need explanation regarding which part? Here the first part is the JSF xml code and the corresponding Validatior class. In the original post you may see the code part for adfc-config.xml where the Validator class is defined as below:

      <faces-config version=”1.2″ xmlns=”http://java.sun.com/xml/ns/javaee”>

      oracle.adf.rich

      DateToFromValidator
      view.DateToFromValidator

      If you are new please go through the JDeveloper & ADF 12c – First Application (Fusion Web Application) – HR Schema.

Leave a comment