diff --git a/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/ShortRangeFieldValidator.java b/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/ShortRangeFieldValidator.java
index ab82977c0..b5d958c40 100644
--- a/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/ShortRangeFieldValidator.java
+++ b/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/ShortRangeFieldValidator.java
@@ -15,7 +15,6 @@
*/
package com.opensymphony.xwork2.validator.validators;
-
/**
*
* Field Validator that checks if the short specified is within a certain range.
@@ -24,33 +23,47 @@ package com.opensymphony.xwork2.validator.validators;
*
*
*
- * - fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required
- * - min - the minimum value (if none is specified, it will not be checked)
- * - max - the maximum value (if none is specified, it will not be checked)
+ * - fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required
+ * - min - the minimum value (if none is specified, it will not be checked)
+ * - max - the maximum value (if none is specified, it will not be checked)
*
+ *
+ * The min / max value can be specified as an expression, but then you must also enable parsing it by specifying parse param
+ * as in the example below.
+ * WARNING! Do not use ${min} and ${max} as an expression as this will turn into infinitive loop!
+ *
*
- *
- *
+ *
*
*
- * <validators>
- * <!-- Plain Validator Syntax -->
- * <validator type="short">
- * <param name="fieldName">age</param>
- * <param name="min">20</param>
- * <param name="max">50</param>
- * <message>Age needs to be between ${min} and ${max}</message>
- * </validator>
- *
- * <!-- Field Validator Syntax -->
- * <field name="age">
- * <field-validator type="short">
- * <param name="min">20</param>
- * <param name="max">50</param>
- * <message>Age needs to be between ${min} and ${max}</message>
- * </field-validator>
- * </field>
- * </validators>
+ * <validators>
+ * <!-- Plain Validator Syntax -->
+ * <validator type="short">
+ * <param name="fieldName">age</param>
+ * <param name="min">20</param>
+ * <param name="max">50</param>
+ * <message>Age needs to be between ${min} and ${max}</message>
+ * </validator>
+ *
+ * <!-- Field Validator Syntax -->
+ * <field name="age">
+ * <field-validator type="short">
+ * <param name="min">20</param>
+ * <param name="max">50</param>
+ * <message>Age needs to be between ${min} and ${max}</message>
+ * </field-validator>
+ * </field>
+ *
+ * <!-- Field Validator Syntax with expression -->
+ * <field name="age">
+ * <field-validator type="short">
+ * <param name="parse">true</param>
+ * <param name="${minValue}">20</param> <!-- will be evaluated as: Short getMinValue() -->
+ * <param name="${maxValue}">50</param> <!-- will be evaluated as: Short getMaxValue() -->
+ * <message>Age needs to be between ${min} and ${max}</message>
+ * </field-validator>
+ * </field>
+ * </validators>
*
*
*
@@ -58,35 +71,43 @@ package com.opensymphony.xwork2.validator.validators;
*
* @version $Date$
*/
-public class ShortRangeFieldValidator extends AbstractRangeValidator {
+public class ShortRangeFieldValidator extends AbstractRangeValidator {
- Short max = null;
- Short min = null;
+ String max = null;
+ String min = null;
-
- public void setMax(Short max) {
+ public void setMax(String max) {
this.max = max;
}
- public Short getMax() {
- return max;
+ public String getMax() {
+ return safeConditionalParse(max);
}
@Override
- public Comparable getMaxComparatorValue() {
- return max;
+ public Short getMaxComparatorValue() {
+ return parseShort(getMax());
}
- public void setMin(Short min) {
+ public void setMin(String min) {
this.min = min;
}
- public Short getMin() {
- return min;
+ public String getMin() {
+ return safeConditionalParse(min);
}
@Override
- public Comparable getMinComparatorValue() {
- return min;
+ public Short getMinComparatorValue() {
+ return parseShort(getMin());
}
+
+ private Short parseShort(String value) {
+ if (value != null) {
+ return Short.parseShort(value);
+ } else {
+ return null;
+ }
+ }
+
}
diff --git a/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidatorTest.java b/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidatorTest.java
index 7a6c3df75..a5b6635cf 100644
--- a/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidatorTest.java
+++ b/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/IntRangeFieldValidatorTest.java
@@ -51,23 +51,23 @@ public class IntRangeFieldValidatorTest extends XWorkTestCase {
private ValidationAction prepareAction(int intRange) {
ValidationAction action = new ValidationAction();
- action.setMaxValue(101);
- action.setMinValue(99);
+ action.setIntMaxValue(101);
+ action.setIntMinValue(99);
action.setIntRange(intRange);
return action;
}
private IntRangeFieldValidator prepareValidator(ValidationAction action, ValidatorContext context) {
IntRangeFieldValidator validator = new IntRangeFieldValidator();
- validator.setMax("${maxValue}");
- validator.setMin("${minValue}");
+ validator.setMax("${intMaxValue}");
+ validator.setMin("${intMinValue}");
ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
valueStack.push(action);
validator.setValueStack(valueStack);
validator.setValidatorContext(context);
validator.setFieldName("intRange");
validator.setParse(true);
- validator.setDefaultMessage("Max is ${maxValue}, min is ${minValue} but value is ${intRange}");
+ validator.setDefaultMessage("Max is ${intMaxValue}, min is ${intMinValue} but value is ${intRange}");
return validator;
}
diff --git a/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ShortRangeFieldValidatorTest.java b/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ShortRangeFieldValidatorTest.java
new file mode 100644
index 000000000..a71b75a17
--- /dev/null
+++ b/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ShortRangeFieldValidatorTest.java
@@ -0,0 +1,74 @@
+package com.opensymphony.xwork2.validator.validators;
+
+import com.opensymphony.xwork2.XWorkTestCase;
+import com.opensymphony.xwork2.util.ValueStack;
+import com.opensymphony.xwork2.util.ValueStackFactory;
+import com.opensymphony.xwork2.validator.GenericValidatorContext;
+import com.opensymphony.xwork2.validator.ValidatorContext;
+
+public class ShortRangeFieldValidatorTest extends XWorkTestCase {
+
+ public void testPassValidation() throws Exception {
+ // given
+ ValidationAction action = prepareAction((short) 5);
+ ValidatorContext context = new GenericValidatorContext(action);
+ ShortRangeFieldValidator validator = prepareValidator(action, context);
+
+ // when
+ validator.validate(action);
+
+ // then
+ assertTrue(context.getFieldErrors().size() == 0);
+ }
+
+ public void testMinValidation() throws Exception {
+ // given
+ ValidationAction action = prepareAction((short) 1);
+ ValidatorContext context = new GenericValidatorContext(action);
+ ShortRangeFieldValidator validator = prepareValidator(action, context);
+
+ // when
+ validator.validate(action);
+
+ // then
+ assertTrue(context.getFieldErrors().size() == 1);
+ assertEquals("Max is 10, min is 2 but value is 1", context.getFieldErrors().get("shortRange").get(0));
+ }
+
+ public void testMaxValidation() throws Exception {
+ // given
+ ValidationAction action = prepareAction((short) 11);
+ ValidatorContext context = new GenericValidatorContext(action);
+ ShortRangeFieldValidator validator = prepareValidator(action, context);
+
+ // when
+ validator.validate(action);
+
+ // then
+ assertTrue(context.getFieldErrors().size() == 1);
+ assertEquals("Max is 10, min is 2 but value is 11", context.getFieldErrors().get("shortRange").get(0));
+ }
+
+ private ValidationAction prepareAction(short range) {
+ ValidationAction action = new ValidationAction();
+ action.setShortMaxValue((short) 10);
+ action.setShortMinValue((short) 2);
+ action.setShortRange(range);
+ return action;
+ }
+
+ private ShortRangeFieldValidator prepareValidator(ValidationAction action, ValidatorContext context) {
+ ShortRangeFieldValidator validator = new ShortRangeFieldValidator();
+ validator.setMax("${shortMaxValue}");
+ validator.setMin("${shortMinValue}");
+ ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
+ valueStack.push(action);
+ validator.setValueStack(valueStack);
+ validator.setValidatorContext(context);
+ validator.setFieldName("shortRange");
+ validator.setParse(true);
+ validator.setDefaultMessage("Max is ${shortMaxValue}, min is ${shortMinValue} but value is ${shortRange}");
+ return validator;
+ }
+
+}
diff --git a/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidationAction.java b/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidationAction.java
index 1e11cbb23..1999f166e 100644
--- a/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidationAction.java
+++ b/xwork-core/src/test/java/com/opensymphony/xwork2/validator/validators/ValidationAction.java
@@ -3,8 +3,12 @@ package com.opensymphony.xwork2.validator.validators;
public class ValidationAction {
private Integer intRange;
- private Integer minValue;
- private Integer maxValue;
+ private Integer intMinValue;
+ private Integer intMaxValue;
+
+ private Short shortRange;
+ private Short shortMinValue;
+ private Short shortMaxValue;
public Integer getIntRange() {
return intRange;
@@ -14,19 +18,43 @@ public class ValidationAction {
this.intRange = intRange;
}
- public Integer getMinValue() {
- return minValue;
+ public Integer getIntMinValue() {
+ return intMinValue;
}
- public void setMinValue(Integer minValue) {
- this.minValue = minValue;
+ public void setIntMinValue(Integer intMinValue) {
+ this.intMinValue = intMinValue;
}
- public Integer getMaxValue() {
- return maxValue;
+ public Integer getIntMaxValue() {
+ return intMaxValue;
}
- public void setMaxValue(Integer maxValue) {
- this.maxValue = maxValue;
+ public void setIntMaxValue(Integer intMaxValue) {
+ this.intMaxValue = intMaxValue;
+ }
+
+ public Short getShortRange() {
+ return shortRange;
+ }
+
+ public void setShortRange(Short shortRange) {
+ this.shortRange = shortRange;
+ }
+
+ public Short getShortMinValue() {
+ return shortMinValue;
+ }
+
+ public void setShortMinValue(Short shortMinValue) {
+ this.shortMinValue = shortMinValue;
+ }
+
+ public Short getShortMaxValue() {
+ return shortMaxValue;
+ }
+
+ public void setShortMaxValue(Short shortMaxValue) {
+ this.shortMaxValue = shortMaxValue;
}
}