rleland     2002/10/11 12:22:58

  Modified:    src/share/org/apache/struts/util StrutsValidator.java
  Log:
  Buzilla [10191]
  Allow Ranges for Floating point numbers
  
  Revision  Changes    Path
  1.9       +50 -1     
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.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- StrutsValidator.java      10 Oct 2002 17:51:20 -0000      1.8
  +++ StrutsValidator.java      11 Oct 2002 19:22:58 -0000      1.9
  @@ -592,11 +592,60 @@
   
          if (!GenericValidator.isBlankOrNull(value)) {
              try {
  -               double dValue = Integer.parseInt(value);
  +               double dValue = Double.parseDouble(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;
  +   }
  +
  +   /**
  +    *  <p>
  +    *
  +    *  Checks if a fields value is within a range (min &amp; 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 validateFloatRange(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 {
  +               float fValue = Float.parseFloat(value);
  +               float min = Float.parseFloat(sMin);
  +               float max = Float.parseFloat(sMax);
  +
  +               if (!GenericValidator.isInRange(fValue, min, max)) {
                      errors.add(field.getKey(), 
StrutsValidatorUtil.getActionError(request, va, field));
   
                      return false;
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to