|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.enhydra.barracuda.core.forms.AbstractFormValidator | +--org.enhydra.barracuda.core.forms.DefaultFormValidator | +--org.enhydra.barracuda.core.forms.validators.DateRangeValidator
This validator ensures that a value is within a given date range
Field Summary | |
protected java.util.Date |
myEndDate
|
protected java.util.Date |
myStartDate
|
Fields inherited from class org.enhydra.barracuda.core.forms.DefaultFormValidator |
localLogger, validators |
Constructor Summary | |
DateRangeValidator()
Public no-args constructor. |
|
DateRangeValidator(java.util.Date theStartDate,
java.util.Date theEndDate)
Public constructor. |
|
DateRangeValidator(java.util.Date theStartDate,
java.util.Date theEndDate,
java.lang.String theErrMsg)
Public constructor. |
Method Summary | |
java.util.Date |
getEndDate()
Return the maximum date value |
java.util.Date |
getStartDate()
Return the minimum date value |
void |
validateFormElement(java.lang.Object val,
FormElement element,
boolean deferExceptions)
Validate a FormElement to make see if the element equals() a given object |
Methods inherited from class org.enhydra.barracuda.core.forms.DefaultFormValidator |
addValidator, getValidators, removeValidator, validate, validateForm, validateFormElement |
Methods inherited from class org.enhydra.barracuda.core.forms.AbstractFormValidator |
generateException, getErrorMessage, isNull, setErrorMessage |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
protected java.util.Date myStartDate
protected java.util.Date myEndDate
Constructor Detail |
public DateRangeValidator()
public DateRangeValidator(java.util.Date theStartDate, java.util.Date theEndDate)
theStartDate
- the low end of the rangetheEndDate
- the high end of the rangepublic DateRangeValidator(java.util.Date theStartDate, java.util.Date theEndDate, java.lang.String theErrMsg)
theStartDate
- the low end of the rangetheEndDate
- the high end of the rangetheErrMsg
- the message associated with this errorMethod Detail |
public java.util.Date getStartDate()
public java.util.Date getEndDate()
public void validateFormElement(java.lang.Object val, FormElement element, boolean deferExceptions) throws ValidationException
validateFormElement
in class DefaultFormValidator
element
- the form element to be validateddeferExceptions
- do we want to deferValidation exceptions
and attempt to validate all elements so that we can process
all the exceptions at onceval
- the actual value to be validated
ValidationException
- if the element is not valid
public void validateFormElement(Object val, FormElement element, boolean deferExceptions) throws ValidationException {
//eliminate the obvious
if (myStartDate==null && myEndDate==null) {
throw this.generateException(element,
deferExceptions,
"Invalid range:"+myStartDate+" to "+myEndDate);
}
// cannot validate a null or empty string value, use NotNullvalidator if you want to disallow nulls
if ( val==null || val.toString().length() == 0 ) return;
//figure out what type of element we're dealing with
if (localLogger.isInfoEnabled()) localLogger.info("validating val="+val+" is in range between "+myStartDate+" and "+myEndDate);
FormType formType = element.getType();
//if element is null, or not form type=STRING or DATE, err because this should
//not be considered a valid option (note that these exceptions are always
//immediate since they typically represent a programming error)
if ( element==null ) throw new ValidationException(val, "Object val:"+val+" is associated with a null FormElement");
if ( !(formType.equals(FormType.DATE) || formType.equals(FormType.STRING)) ) {
throw new ValidationException(val, "Unsupported validation: "+
val+" is of FormType " +
formType.toString() +
" and cannot be validated by this validator");
}
Date dateVal = null;
if ( formType.equals(FormType.DATE) ) {
try {
dateVal = (Date)val;
} catch ( ClassCastException ex ) {
throw this.generateException(element,
deferExceptions,
val.toString() + " is not a valid date class");
}
} else {
// attempt to convert the string to a date
try {
dateVal = (Date)(FormType.DATE.parse( val.toString() ));
} catch ( ParseException ex ) {
throw this.generateException(
element,
deferExceptions,
val.toString() + " is not a valid date string");
}
}
String defaultErrMsg = null;
if (myEndDate==null) {
defaultErrMsg = "Value: "+val+ " is not after "+this.myStartDate;
} else if (myStartDate==null) {
defaultErrMsg = "Value: "+val+ " is not before "+this.myEndDate;
} else {
defaultErrMsg = "Value: "+val+ " is not in the range between "+this.myStartDate+" and "+this.myEndDate;
}
if ( null != this.myStartDate && dateVal.before(this.myStartDate) ) {
throw this.generateException(element, deferExceptions, defaultErrMsg);
}
if ( null != this.myEndDate && dateVal.after(this.myEndDate) ) {
throw this.generateException(element, deferExceptions, defaultErrMsg);
}
}
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |