You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2013/01/02 22:42:12 UTC

svn commit: r1428073 - in /struts/struts2/trunk: core/src/main/resources/template/xhtml/ xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/ xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/

Author: lukaszlenart
Date: Wed Jan  2 21:42:12 2013
New Revision: 1428073

URL: http://svn.apache.org/viewvc?rev=1428073&view=rev
Log:
WW-3888 uses new way to define min/max as expression

Modified:
    struts/struts2/trunk/core/src/main/resources/template/xhtml/form-close-validate.ftl
    struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidator.java
    struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidatorTest.java

Modified: struts/struts2/trunk/core/src/main/resources/template/xhtml/form-close-validate.ftl
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/resources/template/xhtml/form-close-validate.ftl?rev=1428073&r1=1428072&r2=1428073&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/main/resources/template/xhtml/form-close-validate.ftl (original)
+++ struts/struts2/trunk/core/src/main/resources/template/xhtml/form-close-validate.ftl Wed Jan  2 21:42:12 2013
@@ -98,10 +98,10 @@ END SNIPPET: supported-validators
             }
             <#elseif validator.validatorType = "int">
             if (continueValidation && field.value != null) {
-                if (<#if validator.min??>parseInt(field.value) <
-                     ${validator.min}<#else>false</#if> ||
-                        <#if validator.max??>parseInt(field.value) >
-                           ${validator.max}<#else>false</#if>) {
+                if (<#if validator.minComparatorValue??>parseInt(field.value) <
+                     ${validator.minComparatorValue?c}<#else>false</#if> ||
+                        <#if validator.maxComparatorValue??>parseInt(field.value) >
+                           ${validator.maxComparatorValue?c}<#else>false</#if>) {
                     addError(field, error);
                     errors = true;
                     <#if validator.shortCircuit>continueValidation = false;</#if>

Modified: struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidator.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidator.java?rev=1428073&r1=1428072&r2=1428073&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidator.java (original)
+++ struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidator.java Wed Jan  2 21:42:12 2013
@@ -19,25 +19,28 @@ package com.opensymphony.xwork2.validato
  * <!-- START SNIPPET: javadoc -->
  * Field Validator that checks if the integer specified is within a certain range.
  * <!-- END SNIPPET: javadoc -->
- * 
- * 
+ *
+ *
  * <!-- START SNIPPET: parameters -->
  * <ul>
  * 		<li>fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required</li>
  * 		<li>min - the minimum value (if none is specified, it will not be checked) </li>
  * 		<li>max - the maximum value (if none is specified, it will not be checked) </li>
+ *      <li>parse - if set to true, minExpression and maxExpression will be evaluated to find min/max</li>
+ *      <li>minExpression - expression to calculate the minimum value (if none is specified, it will not be checked) </li>
+ *      <li>maxExpression - expression to calculate the maximum value (if none is specified, it will not be checked) </li>
  * </ul>
  *
- * The min / max value can be specified as an expression, but then you must also enable parsing it by specifying <strong>parse</strong> param
- * as in the example below.
- * WARNING! Do not use ${min} and ${max} as an expression as this will turn into infinitive loop!
+ * You can either use the min / max value or minExpression / maxExpression (when parse is set to true) -
+ * using expression can be slightly slower, see the example below.
+ * WARNING! Do not use ${minExpression} and ${maxExpression} as an expression as this will turn into infinitive loop!
  *
  * <!-- END SNIPPET: parameters -->
- * 
- * 
+ *
+ *
  * <pre>
  * <!-- START SNIPPET: examples -->
- *	&lt;validators>
+ * 	&lt;validators>
  *      &lt;!-- Plain Validator Syntax --&gt;
  *      &lt;validator type="int">
  *          &lt;param name="fieldName"&gt;age&lt;/param&gt;
@@ -59,55 +62,75 @@ package com.opensymphony.xwork2.validato
  *      &lt;field name="age"&gt;
  *          &lt;field-validator type="int"&gt;
  *              &lt;param name="parse"&gt;true&lt;/param&gt;
- *              &lt;param name="${minValue}"&gt;20&lt;/param&gt; &lt;!-- will be evaluated as: Integer getMinValue() --&gt;
- *              &lt;param name="${maxValue}"&gt;50&lt;/param&gt; &lt;!-- will be evaluated as: Integer getMaxValue() --&gt;
- *              &lt;message&gt;Age needs to be between ${min} and ${max}&lt;/message&gt;
+ *              &lt;param name="minExpression"&gt;${minValue}&lt;/param&gt; &lt;!-- will be evaluated as: Integer getMinValue() --&gt;
+ *              &lt;param name="maxExpression"&gt;${maxValue}&lt;/param&gt; &lt;!-- will be evaluated as: Integer getMaxValue() --&gt;
+ *              &lt;message&gt;Age needs to be between ${minExpression} and ${maxExpression}&lt;/message&gt;
  *          &lt;/field-validator&gt;
  *      &lt;/field&gt;
  * &lt;/validators&gt;
  * <!-- END SNIPPET: examples -->
  * </pre>
- * 
+ *
  * @author Jason Carreira
  * @version $Date$ $Id$
  */
 public class IntRangeFieldValidator extends AbstractRangeValidator<Integer> {
 
-    private String max = null;
-    private String min = null;
+    private Integer min = null;
+    private Integer max = null;
+    private String minExpression;
+    private String maxExpression;
 
-    public void setMax(String max) {
-        this.max = max;
+    public IntRangeFieldValidator() {
+        super(Integer.class);
     }
 
-    public String getMax() {
-        return safeConditionalParse(max);
+    public void setMin(Integer min) {
+        this.min = min;
     }
 
-    @Override
-    public Integer getMaxComparatorValue() {
-        return parseInt(getMax());
+    public Integer getMin() {
+        return min;
     }
 
-    public void setMin(String min) {
-        this.min = min;
+    public String getMinExpression() {
+        return minExpression;
     }
 
-    public String getMin() {
-        return safeConditionalParse(min);
+    public void setMinExpression(String minExpression) {
+        this.minExpression = minExpression;
     }
 
     @Override
     public Integer getMinComparatorValue() {
-        return parseInt(getMin());
+        if (parse) {
+            return parse(getMinExpression());
+        }
+        return getMin();
     }
 
-    private Integer parseInt(String value) {
-        if (value != null) {
-            return Integer.parseInt(value);
-        } else {
-            return null;
+    public void setMax(Integer max) {
+        this.max = max;
+    }
+
+    public Integer getMax() {
+        return max;
+    }
+
+    public String getMaxExpression() {
+        return maxExpression;
+    }
+
+    public void setMaxExpression(String maxExpression) {
+        this.maxExpression = maxExpression;
+    }
+
+    @Override
+    public Integer getMaxComparatorValue() {
+        if (parse) {
+            return parse(getMaxExpression());
         }
+        return getMax();
     }
 
 }

Modified: struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidatorTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidatorTest.java?rev=1428073&r1=1428072&r2=1428073&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidatorTest.java (original)
+++ struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidatorTest.java Wed Jan  2 21:42:12 2013
@@ -59,8 +59,8 @@ public class IntRangeFieldValidatorTest 
 
     private IntRangeFieldValidator prepareValidator(ValidationAction action, ValidatorContext context) {
         IntRangeFieldValidator validator = new IntRangeFieldValidator();
-        validator.setMax("${intMaxValue}");
-        validator.setMin("${intMinValue}");
+        validator.setMaxExpression("${intMaxValue}");
+        validator.setMinExpression("${intMinValue}");
         ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
         valueStack.push(action);
         validator.setValueStack(valueStack);