tapestry-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mindbri...@apache.org
Subject cvs commit: jakarta-tapestry/framework/src/org/apache/tapestry/valid NumberValidator.script NumberValidator.java
Date Wed, 12 Nov 2003 18:11:10 GMT
mindbridge    2003/11/12 10:11:10

  Modified:    framework/src/org/apache/tapestry/valid
                        NumberValidator.script NumberValidator.java
  Log:
  Fixing a bug with the client-side validation as suggested by Julio C. Rivera
  with some minor modifications by me (mindbridge)
  
  PR: 22958   NumberValidator forces to input a value when you set maximum and/or minimum
but required is false  
  Submitted by:	juceri@ya.com (Julio C. Rivera)
  Reviewed by:	mindbridge
  
  Revision  Changes    Path
  1.4       +15 -3     jakarta-tapestry/framework/src/org/apache/tapestry/valid/NumberValidator.script
  
  Index: NumberValidator.script
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/valid/NumberValidator.script,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- NumberValidator.script	1 Jun 2003 05:05:12 -0000	1.3
  +++ NumberValidator.script	12 Nov 2003 18:11:10 -0000	1.4
  @@ -36,10 +36,22 @@
   function ${function}()
   {
     var field = document.${form.name}.${field.name}; 
  -  var value = field.value;  
  +  var stringValue = field.value;  
   <if expression="validator.required">
  -  if (value.length == 0)
  +  if (stringValue.length == 0)
        return validator_invalid_field(field, "${requiredMessage}");
  +</if>
  +<if-not expression="validator.required">
  +  if (stringValue.length == 0)
  +     return true;
  +</if-not>
  +  var value = stringValue * 1;
  +  if (isNaN(value))
  +      return validator_invalid_field(field, "${formatMessage}");
  +<if expression="validator.integerNumber">
  +  var regex = /\./;
  +  if (stringValue.search(regex) != -1)
  +      return validator_invalid_field(field, "${formatMessage}");
   </if>
   <if expression="validator.minimum != null">
     if (value &lt; ${validator.minimum})
  
  
  
  1.7       +89 -10    jakarta-tapestry/framework/src/org/apache/tapestry/valid/NumberValidator.java
  
  Index: NumberValidator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-tapestry/framework/src/org/apache/tapestry/valid/NumberValidator.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- NumberValidator.java	28 May 2003 13:41:48 -0000	1.6
  +++ NumberValidator.java	12 Nov 2003 18:11:10 -0000	1.7
  @@ -58,7 +58,9 @@
   import java.math.BigDecimal;
   import java.math.BigInteger;
   import java.util.HashMap;
  +import java.util.HashSet;
   import java.util.Map;
  +import java.util.Set;
   
   import org.apache.tapestry.ApplicationRuntimeException;
   import org.apache.tapestry.IMarkupWriter;
  @@ -110,6 +112,8 @@
           TYPES.put("java.math.BigDecimal", BigDecimal.class);
       }
   
  +    private static final Set INT_TYPES = new HashSet();
  +
       private Class _valueTypeClass = int.class;
   
       private boolean _zeroIsNull;
  @@ -119,12 +123,16 @@
       private String _scriptPath = "/org/apache/tapestry/valid/NumberValidator.script";
   
       private String _invalidNumericFormatMessage;
  +    private String _invalidIntegerFormatMessage;
       private String _numberTooSmallMessage;
       private String _numberTooLargeMessage;
       private String _numberRangeMessage;
   
       private static AdaptorRegistry _numberAdaptors = new AdaptorRegistry();
   
  +    private final static int NUMBER_TYPE_INTEGER    = 0;
  +    private final static int NUMBER_TYPE_REAL       = 1;
  +
       private static abstract class NumberAdaptor
       {
           /**
  @@ -136,6 +144,16 @@
   
           abstract public Number parse(String value);
   
  +        /**
  +         *  Indicates the type of the number represented -- integer or real.
  +         *  The information is used to build the client-side validator.  
  +         *  This method could return a boolean, but returns an int to allow
  +         *  future extensions of the validator.
  +         *   
  +         *  @return one of the predefined number types
  +         **/
  +        abstract public int getNumberType();
  +
           public int compare(Number left, Number right)
           {
               Comparable lc = (Comparable) left;
  @@ -144,7 +162,23 @@
           }
       }
   
  -    private static class ByteAdaptor extends NumberAdaptor
  +    private static abstract class IntegerNumberAdaptor extends NumberAdaptor
  +    {
  +        public int getNumberType()
  +        {
  +            return NUMBER_TYPE_INTEGER;
  +        }
  +    }
  +
  +    private static abstract class RealNumberAdaptor extends NumberAdaptor
  +    {
  +        public int getNumberType()
  +        {
  +            return NUMBER_TYPE_REAL;
  +        }
  +    }
  +
  +    private static class ByteAdaptor extends IntegerNumberAdaptor
       {
           public Number parse(String value)
           {
  @@ -152,7 +186,7 @@
           }
       }
   
  -    private static class ShortAdaptor extends NumberAdaptor
  +    private static class ShortAdaptor extends IntegerNumberAdaptor
       {
           public Number parse(String value)
           {
  @@ -160,7 +194,7 @@
           }
       }
   
  -    private static class IntAdaptor extends NumberAdaptor
  +    private static class IntAdaptor extends IntegerNumberAdaptor
       {
           public Number parse(String value)
           {
  @@ -168,7 +202,7 @@
           }
       }
   
  -    private static class LongAdaptor extends NumberAdaptor
  +    private static class LongAdaptor extends IntegerNumberAdaptor
       {
           public Number parse(String value)
           {
  @@ -176,7 +210,7 @@
           }
       }
   
  -    private static class FloatAdaptor extends NumberAdaptor
  +    private static class FloatAdaptor extends RealNumberAdaptor
       {
           public Number parse(String value)
           {
  @@ -184,7 +218,7 @@
           }
       }
   
  -    private static class DoubleAdaptor extends FloatAdaptor
  +    private static class DoubleAdaptor extends RealNumberAdaptor
       {
           public Number parse(String value)
           {
  @@ -192,7 +226,7 @@
           }
       }
   
  -    private static class BigDecimalAdaptor extends FloatAdaptor
  +    private static class BigDecimalAdaptor extends RealNumberAdaptor
       {
           public Number parse(String value)
           {
  @@ -200,7 +234,7 @@
           }
       }
   
  -    private static class BigIntegerAdaptor extends NumberAdaptor
  +    private static class BigIntegerAdaptor extends IntegerNumberAdaptor
       {
           public Number parse(String value)
           {
  @@ -363,7 +397,10 @@
           if (isRequired())
               symbols.put("requiredMessage", buildRequiredMessage(field));
   
  -        symbols.put("formatMessage", buildInvalidNumericFormatMessage(field));
  +        if (isIntegerNumber())
  +            symbols.put("formatMessage", buildInvalidIntegerFormatMessage(field));
  +        else
  +            symbols.put("formatMessage", buildInvalidNumericFormatMessage(field));
   
           if (_minimum != null || _maximum != null)
               symbols.put("rangeMessage", buildRangeMessage(field));
  @@ -455,6 +492,13 @@
   
       /** @since 3.0 */
   
  +    public String getInvalidIntegerFormatMessage()
  +    {
  +        return _invalidIntegerFormatMessage;
  +    }
  +
  +    /** @since 3.0 */
  +
       public String getNumberRangeMessage()
       {
           return _numberRangeMessage;
  @@ -486,6 +530,18 @@
           _invalidNumericFormatMessage = string;
       }
   
  +    /** 
  +     * Overrides the <code>invalid-int-format</code> bundle key.
  +     * Parameter {0} is the display name of the field.
  +     * 
  +     * @since 3.0
  +     */
  +
  +    public void setInvalidIntegerFormatMessage(String string)
  +    {
  +        _invalidIntegerFormatMessage = string;
  +    }
  +
       /** @since 3.0 */
   
       protected String buildInvalidNumericFormatMessage(IFormComponent field)
  @@ -499,6 +555,19 @@
           return formatString(pattern, field.getDisplayName());
       }
   
  +    /** @since 3.0 */
  +
  +    protected String buildInvalidIntegerFormatMessage(IFormComponent field)
  +    {
  +        String pattern =
  +            getPattern(
  +                getInvalidIntegerFormatMessage(),
  +                "invalid-int-format",
  +                field.getPage().getLocale());
  +
  +        return formatString(pattern, field.getDisplayName());
  +    }
  +
       /** 
        * Overrides the <code>number-range</code> bundle key.
        * Parameter [0} is the display name of the field.
  @@ -568,4 +637,14 @@
           return formatString(pattern, field.getDisplayName(), _minimum);
       }
   
  +    /** @since 3.0 */
  +    
  +    public boolean isIntegerNumber()
  +    {
  +        NumberAdaptor result = (NumberAdaptor) _numberAdaptors.getAdaptor(_valueTypeClass);
  +        if (result == null)
  +            return false;
  +        
  +        return result.getNumberType() == NUMBER_TYPE_INTEGER;
  +    }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org


Mime
View raw message