struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject svn commit: r1428576 - in /struts/struts2/trunk/xwork-core/src: main/java/com/opensymphony/xwork2/validator/validators/DoubleRangeFieldValidator.java test/java/com/opensymphony/xwork2/validator/DoubleRangeValidatorTest.java
Date Thu, 03 Jan 2013 20:25:47 GMT
Author: lukaszlenart
Date: Thu Jan  3 20:25:46 2013
New Revision: 1428576

URL: http://svn.apache.org/viewvc?rev=1428576&view=rev
Log:
WW-3955 adds support to specify maxInclusive, minInclusive, minExclusive and maxExclusive
params as expression

Modified:
    struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/DoubleRangeFieldValidator.java
    struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeValidatorTest.java

Modified: struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/DoubleRangeFieldValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/DoubleRangeFieldValidator.java?rev=1428576&r1=1428575&r2=1428576&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/DoubleRangeFieldValidator.java
(original)
+++ struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/DoubleRangeFieldValidator.java
Thu Jan  3 20:25:46 2013
@@ -26,42 +26,52 @@ import com.opensymphony.xwork2.validator
  *
  * <!-- START SNIPPET: parameters -->
  * <ul>
- *                 <li>fieldName - The field name this validator is validating. Required
if using
-Plain-Validator Syntax otherwise not required</li>
- *                 <li>minInclusive - the minimum inclusive value in FloatValue format
specified by Java language (if none is specified, it will
-not be checked) </li>
- *                 <li>maxInclusive - the maximum inclusive value in FloatValue format
specified by Java language (if none is specified, it will
-not be checked) </li>
- *                 <li>minExclusive - the minimum exclusive value in FloatValue format
specified by Java language (if none is specified, it will
-not be checked) </li>
- *                 <li>maxExclusive - the maximum exclusive value in FloatValue format
specified by Java language (if none is specified, it will
-not be checked) </li>
+ *     <li>fieldName - The field name this validator is validating. Required if using
Plain-Validator Syntax otherwise not required</li>
+ *     <li>minInclusive - the minimum inclusive value in FloatValue format specified
by Java language (if none is specified, it will not be checked) </li>
+ *     <li>maxInclusive - the maximum inclusive value in FloatValue format specified
by Java language (if none is specified, it will not be checked) </li>
+ *     <li>minExclusive - the minimum exclusive value in FloatValue format specified
by Java language (if none is specified, it will not be checked) </li>
+ *     <li>maxExclusive - the maximum exclusive value in FloatValue format specified
by Java language (if none is specified, it will not be checked) </li>
  * </ul>
+ *
+ * You can specify minInclusive, maxInclusive, minExclusive and maxExclusive as a OGNL expression,
see example below.
  * <!-- END SNIPPET: parameters -->
  *
  *
+ * <!-- START SNIPPET: parameters-warning -->
+ * Do not use ${minInclusive}, ${maxInclusive}, ${minExclusive} and ${maxExclusive} as an
expression as this will turn into infinitive loop!
+ * <!-- END SNIPPET: parameters-warning -->
+ *
+ *
  * <pre>
  * <!-- START SNIPPET: examples -->
- *                 &lt;validators>
- *           &lt;!-- Plain Validator Syntax --&gt;
- *           &lt;validator type="double">
- *               &lt;param name="fieldName"&gt;percentage&lt;/param&gt;
- *               &lt;param name="minInclusive"&gt;20.1&lt;/param&gt;
- *               &lt;param name="maxInclusive"&gt;50.1&lt;/param&gt;
- *               &lt;message&gt;Age needs to be between ${minInclusive} and
-${maxInclusive} (inclusive)&lt;/message&gt;
- *           &lt;/validator&gt;
- *
- *           &lt;!-- Field Validator Syntax --&gt;
- *           &lt;field name="percentage"&gt;
- *               &lt;field-validator type="double"&gt;
- *                   &lt;param name="minExclusive"&gt;0.123&lt;/param&gt;
- *                   &lt;param name="maxExclusive"&gt;99.98&lt;/param&gt;
- *                   &lt;message&gt;Percentage needs to be between ${minExclusive}
-and ${maxExclusive} (exclusive)&lt;/message&gt;
- *               &lt;/field-validator&gt;
- *           &lt;/field&gt;
- *      &lt;/validators&gt;
+ * &lt;validators>
+ *     &lt;!-- Plain Validator Syntax --&gt;
+ *         &lt;validator type="double">
+ *         &lt;param name="fieldName"&gt;percentage&lt;/param&gt;
+ *         &lt;param name="minInclusive"&gt;20.1&lt;/param&gt;
+ *         &lt;param name="maxInclusive"&gt;50.1&lt;/param&gt;
+ *         &lt;message&gt;Age needs to be between ${minInclusive} and ${maxInclusive}
(inclusive)&lt;/message&gt;
+ *     &lt;/validator&gt;
+ *
+ *     &lt;!-- Field Validator Syntax --&gt;
+ *     &lt;field name="percentage"&gt;
+ *         &lt;field-validator type="double"&gt;
+ *             &lt;param name="minExclusive"&gt;0.123&lt;/param&gt;
+ *             &lt;param name="maxExclusive"&gt;99.98&lt;/param&gt;
+ *             &lt;message&gt;Percentage needs to be between ${minExclusive} and
${maxExclusive} (exclusive)&lt;/message&gt;
+ *         &lt;/field-validator&gt;
+ *     &lt;/field&gt;
+ *
+ *     &lt;!-- Field Validator Syntax with expression --&gt;
+ *     &lt;field name="percentage"&gt;
+ *         &lt;field-validator type="double"&gt;
+ *             &lt;param name="parse"&gt;true&lt;/param&gt;
+ *             &lt;param name="minExclusive"&gt;${minExclusiveValue}&lt;/param&gt;
&lt;!-- will be evaluated as: Double getMinExclusiveValue() --&gt;
+ *             &lt;param name="maxExclusive"&gt;${maxExclusive}&lt;/param&gt;
&lt;!-- will be evaluated as: Double getMaxExclusive() --&gt;
+ *             &lt;message&gt;Percentage needs to be between ${minExclusive} and
${maxExclusive} (exclusive)&lt;/message&gt;
+ *         &lt;/field-validator&gt;
+ *     &lt;/field&gt;
+ * &lt;/validators&gt;
  * <!-- END SNIPPET: examples -->
  * </pre>
  *
@@ -70,7 +80,6 @@ and ${maxExclusive} (exclusive)&lt;/mess
  *
  * @version $Id$
  */
-// START SNIPPET: field-level-validator
 public class DoubleRangeFieldValidator extends FieldValidatorSupport {
     
     String maxInclusive = null;
@@ -105,14 +114,22 @@ public class DoubleRangeFieldValidator e
         }
     }
 
-    private void parseParameterValues() {
-        this.minInclusiveValue = parseDouble(minInclusive);
-        this.maxInclusiveValue = parseDouble(maxInclusive);
-        this.minExclusiveValue = parseDouble(minExclusive);
-        this.maxExclusiveValue = parseDouble(maxExclusive);
+    protected void parseParameterValues() {
+        this.minInclusiveValue = parseValue(minInclusive);
+        this.maxInclusiveValue = parseValue(maxInclusive);
+        this.minExclusiveValue = parseValue(minExclusive);
+        this.maxExclusiveValue = parseValue(maxExclusive);
     }
 
-    private Double parseDouble (String value) {
+    protected Double parseValue(String value) {
+        if (parse) {
+            return (Double) parse(value, Double.class);
+        } else {
+            return parseDouble(value);
+        }
+    }
+
+    protected Double parseDouble (String value) {
         if (value != null) {
             try {
                 return Double.valueOf(value);
@@ -156,5 +173,5 @@ public class DoubleRangeFieldValidator e
     public void setMaxExclusive(String maxExclusive) {
         this.maxExclusive = maxExclusive;
     }
+
 }
-// END SNIPPET: field-level-validator

Modified: struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeValidatorTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeValidatorTest.java?rev=1428576&r1=1428575&r2=1428576&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeValidatorTest.java
(original)
+++ struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeValidatorTest.java
Thu Jan  3 20:25:46 2013
@@ -187,6 +187,35 @@ public class DoubleRangeValidatorTest ex
         assertTrue(!context.hasErrors()); // should pass as null value passed in
     }
 
+    public void testExpressionParams() throws Exception {
+        ValueStack stack = ActionContext.getContext().getValueStack();
+        ActionSupport action = new ActionSupport() {
+
+            public Double getMinInclusiveValue() {return 10d;}
+            public Double getMaxInclusiveValue() {return 11d;}
+            public Double getMinExclusiveValue() {return 13d;}
+            public Double getMaxExclusiveValue() {return 14d;}
+            public Double getPrice() {return 15d;}
+        };
+
+        stack.push(action);
+
+        val.setParse(true);
+        val.setMinInclusive("${minInclusiveValue}");
+        val.setMaxInclusive("${maxInclusiveValue}");
+        val.setMinExclusive("${minExclusiveValue}");
+        val.setMaxExclusive("${maxExclusiveValue}");
+
+        val.setFieldName("price");
+        val.setDefaultMessage("Price is wrong!");
+
+        DelegatingValidatorContext context = new DelegatingValidatorContext(action);
+        val.setValidatorContext(context);
+
+        val.validate(action);
+        assertTrue(action.getFieldErrors().get("price").size() == 1);
+    }
+
     @Override
     protected void setUp() throws Exception {
         super.setUp();



Mime
View raw message