How to Use the ICEfaces selectInputDate ComponentApplications frequently require users to insert a date such as Date-of-Birth or the Start Date of employment. The ice:selectInputDate component offers a user safe and convenient way of entering a date without worrying about format, locale and other features.
This tutorial will discuss the following topics related to the use of the ice:selectInputDate component: Creating a Simple Calendar
Calendar component usage in the page: <ice:selectInputDate id="date1" value="#{dateSelect.date1}"/>
The selected date is stored in the backing bean 'date1' property (type java.util.Date).
Creating a Popup Calendar
In order to use a popup calendar, set the 'renderAsPopup' attribute to 'true': <ice:selectInputDate id="date2" value="#{dateSelect.date2}" renderAsPopup="true"/>
Using Converters and Validators
A converter can be used to format how the Date is rendered out to the browser: <ice:selectInputDate id="date1" value="#{dateSelect.date1}"> <f:convertDateTime pattern="MM/dd/yyyy" timeZone="#{dateSelect.timeZone}"/> </ice:selectInputDate> In this example, we specify a common North American date presentation of MM/dd/yyyy. We also chose a time zone using the standard java TimeZone class. By default, a java.util.Date is in the GMT/UTC time zone. It is a best practice to keep them in this time zone in your back end. However, when the date is output to the GUI, you may want to apply time zones for different users in different regions of the world. At this point you can format the date appropriately using the 'timeZone' attribute. Often programmers need to place restrictions which dates can be selected. If it's a birthday it must be in the past, if this is a flight it must be in the future, and so on. Validators are used to restrict user input: <ice:selectInputDate id="date1" value="#{dateSelect.date1}"> <f:convertDateTime pattern="MM/dd/yyyy" timeZone="#{dateSelect.timeZone}"/> <f:validator validatorId="dateRangeValidator" /> </ice:selectInputDate> The following code is the 'validate' method called from the dateRangeValidator. The 'value' parameter of validate() is a Date object. This method constructs a JSF FacesMessage to be displayed when validation fails: DateRangeValidator.java public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { Date myDate = (Date)value; Date today = new Date(); if (myDate.before(today)) { FacesMessage message = new FacesMessage(); message.setDetail("Date is in the past"); message.setSummary("Date is in the past"); message.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ValidatorException(message); } }
Customizing Calendar Style
Tutorial Source Code Downloads
|
SelectInputDate
© Copyright 2021 ICEsoft Technologies Canada Corp.