How would I create custom data type to a UIComponent attribute?
Example: Suppose one has a UIInputDate
(an UIInput
) and has an attribute Date maxDate
, how will I make sure that whatever maxDate
is entered will always be resulted as Date
?
How would I create custom data type to a UIComponent attribute?
Example: Suppose one has a UIInputDate
(an UIInput
) and has an attribute Date maxDate
, how will I make sure that whatever maxDate
is entered will always be resulted as Date
?
You can just create a custom validator the usual way. The input component is already available as 2nd argument, you just have to cast it.
public class UIInputDateRangeValidator implements Validator {
public void validate(FacesContext context, UIComponent component, Object value) {
UIInputDate inputDate = (UIInputDate) component;
Date minDate = inputDate.getMinDate();
Date maxDate = inputDate.getMaxDate();
Date date = (Date) value;
// ... Use Date#after(), Date#before(), etc.
}
}
You can create and add the validator in custom component's constructor.
public UIInputDate() {
addValidator(new UIInputDateRangeValidator());
// You can use setConverter() with new DateTimeConverter() if you didn't already do that.
}