rleland 2002/10/10 10:51:20 Modified: src/share/org/apache/struts/util StrutsValidator.java Log: Add validateIntRange() & validateDoubleRange() and depreciate validateRange(). Revision Changes Path 1.8 +100 -3 jakarta-struts/src/share/org/apache/struts/util/StrutsValidator.java Index: StrutsValidator.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/StrutsValidator.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- StrutsValidator.java 25 Aug 2002 04:31:15 -0000 1.7 +++ StrutsValidator.java 10 Oct 2002 17:51:20 -0000 1.8 @@ -57,7 +57,6 @@ import java.io.Serializable; import java.util.Date; import java.util.Locale; -import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -80,7 +79,7 @@ * *@author David Winterfeldt *@author James Turner - *@created March 18, 2002 + *@author Rob Leland *@since Struts 1.1 */ public class StrutsValidator implements Serializable { @@ -469,7 +468,7 @@ * * Checks if a fields value is within a range (min & max specified in the * vars attribute).</p> - * + *@deprecated As of Struts 1.1, replaced by {@link #validateIntRange(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)} *@param bean The bean validation is being performed on. *@param va The <code>ValidatorAction</code> that is currently being performed. *@param field The <code>Field</code> object associated with the current @@ -512,6 +511,104 @@ return true; } + + /** + * <p> + * + * Checks if a fields value is within a range (min & max specified in the + * vars attribute).</p> + * + *@param bean The bean validation is being performed on. + *@param va The <code>ValidatorAction</code> that is currently being performed. + *@param field The <code>Field</code> object associated with the current + * field being validated. + *@param errors The <code>ActionErrors</code> object to add errors to if any + * validation errors occur. + *@param request Current request object. + *@return True if in range, false otherwise. + */ + public static boolean validateIntRange(Object bean, + ValidatorAction va, Field field, + ActionErrors errors, + HttpServletRequest request) { + + String value = null; + if (isString(bean)) { + value = (String) bean; + } else { + value = ValidatorUtil.getValueAsString(bean, field.getProperty()); + } + String sMin = field.getVarValue("min"); + String sMax = field.getVarValue("max"); + + if (!GenericValidator.isBlankOrNull(value)) { + try { + int iValue = Integer.parseInt(value); + int min = Integer.parseInt(sMin); + int max = Integer.parseInt(sMax); + + if (!GenericValidator.isInRange(iValue, min, max)) { + errors.add(field.getKey(), StrutsValidatorUtil.getActionError(request, va, field)); + + return false; + } + } catch (Exception e) { + errors.add(field.getKey(), StrutsValidatorUtil.getActionError(request, va, field)); + return false; + } + } + + return true; + } + + /** + * <p> + * + * Checks if a fields value is within a range (min & max specified in the + * vars attribute).</p> + * + *@param bean The bean validation is being performed on. + *@param va The <code>ValidatorAction</code> that is currently being performed. + *@param field The <code>Field</code> object associated with the current + * field being validated. + *@param errors The <code>ActionErrors</code> object to add errors to if any + * validation errors occur. + *@param request Current request object. + *@return True if in range, false otherwise. + */ + public static boolean validateDoubleRange(Object bean, + ValidatorAction va, Field field, + ActionErrors errors, + HttpServletRequest request) { + + String value = null; + if (isString(bean)) { + value = (String) bean; + } else { + value = ValidatorUtil.getValueAsString(bean, field.getProperty()); + } + String sMin = field.getVarValue("min"); + String sMax = field.getVarValue("max"); + + if (!GenericValidator.isBlankOrNull(value)) { + try { + double dValue = Integer.parseInt(value); + double min = Double.parseDouble(sMin); + double max = Double.parseDouble(sMax); + + if (!GenericValidator.isInRange(dValue, min, max)) { + errors.add(field.getKey(), StrutsValidatorUtil.getActionError(request, va, field)); + + return false; + } + } catch (Exception e) { + errors.add(field.getKey(), StrutsValidatorUtil.getActionError(request, va, field)); + return false; + } + } + + return true; + } /**
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>