LaterThanValidatorpublic class LaterThanValidator extends Object implements Serializable, javax.faces.validator.ValidatorThis class is a JSF Validator that validates that the java.util.Date
value of the component it's attached to is later than the
java.util.Date value of another component. |
Fields Summary |
---|
private String | peerId |
Methods Summary |
---|
private java.lang.String | formatDate(java.util.Date date, javax.faces.context.FacesContext context, javax.faces.component.UIComponent component)Returns the provided Date formatted with the converter attached
to the component, if any, or with toString() otherwise.
Converter converter = ((ValueHolder) component).getConverter();
if (converter != null) {
return converter.getAsString(context, component, date);
}
return date.toString();
| public void | setPeerId(java.lang.String peerId)Sets the ID for the peer component the main component's
value is compared to. It must be a format that can be
used with the javax.faces.component.UIComponent findComponent()
method.
this.peerId = peerId;
| public void | validate(javax.faces.context.FacesContext context, javax.faces.component.UIComponent component, java.lang.Object value)Compares the provided value to the value of the peer component,
and throws a ValidatorException with an appropriate FacesMessage
if the provided value doesn't represent a later date or if
there are problems accessing the peer component value.
Application application = context.getApplication();
/* Note! This validator requires an application bundle
* that holds the messages. For validators, or other
* components, that should be usable in any application,
* bundling a default bundle and use it if the message
* isn't available in the application bundle is a nicer
* approach.
*/
String messageBundleName = application.getMessageBundle();
Locale locale = context.getViewRoot().getLocale();
ResourceBundle rb =
ResourceBundle.getBundle(messageBundleName, locale);
UIComponent peerComponent = component.findComponent(peerId);
if (peerComponent == null) {
String msg = rb.getString("peer_not_found");
FacesMessage facesMsg =
new FacesMessage(FacesMessage.SEVERITY_FATAL, msg, msg);
throw new ValidatorException(facesMsg);
}
ValueHolder peer = null;
try {
peer = (ValueHolder) peerComponent;
}
catch (ClassCastException e) {
String msg = rb.getString("invalid_component");
FacesMessage facesMsg =
new FacesMessage(FacesMessage.SEVERITY_FATAL, msg, msg);
throw new ValidatorException(facesMsg);
}
if (peer instanceof EditableValueHolder &&
!((EditableValueHolder) peer).isValid()) {
// No point in validating against an invalid value
return;
}
Object peerValue = peer.getValue();
if (!(peerValue instanceof Date)) {
String msg = rb.getString("invalid_peer");
FacesMessage facesMsg =
new FacesMessage(FacesMessage.SEVERITY_FATAL, msg, msg);
throw new ValidatorException(facesMsg);
}
if (!(value instanceof Date) ||
!((Date) value).after((Date) peerValue)) {
String msg = rb.getString("not_later");
String detailMsgPattern = rb.getString("not_later_detail");
Object[] params =
{formatDate((Date) peerValue, context, component)};
String detailMsg =
MessageFormat.format(detailMsgPattern, params);
FacesMessage facesMsg =
new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, detailMsg);
throw new ValidatorException(facesMsg);
}
|
|