diff --git a/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/DoubleRangeFieldValidator.java b/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/DoubleRangeFieldValidator.java index 39ffe30f0..67f4ddc10 100644 --- a/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/DoubleRangeFieldValidator.java +++ b/xwork-core/src/main/java/com/opensymphony/xwork2/validator/validators/DoubleRangeFieldValidator.java @@ -26,42 +26,52 @@ import com.opensymphony.xwork2.validator.ValidationException; * * *
*
- * <validators>
- * <!-- Plain Validator Syntax -->
- * <validator type="double">
- * <param name="fieldName">percentage</param>
- * <param name="minInclusive">20.1</param>
- * <param name="maxInclusive">50.1</param>
- * <message>Age needs to be between ${minInclusive} and
-${maxInclusive} (inclusive)</message>
- * </validator>
+ * <validators>
+ * <!-- Plain Validator Syntax -->
+ * <validator type="double">
+ * <param name="fieldName">percentage</param>
+ * <param name="minInclusive">20.1</param>
+ * <param name="maxInclusive">50.1</param>
+ * <message>Age needs to be between ${minInclusive} and ${maxInclusive} (inclusive)</message>
+ * </validator>
*
- * <!-- Field Validator Syntax -->
- * <field name="percentage">
- * <field-validator type="double">
- * <param name="minExclusive">0.123</param>
- * <param name="maxExclusive">99.98</param>
- * <message>Percentage needs to be between ${minExclusive}
-and ${maxExclusive} (exclusive)</message>
- * </field-validator>
- * </field>
- * </validators>
+ * <!-- Field Validator Syntax -->
+ * <field name="percentage">
+ * <field-validator type="double">
+ * <param name="minExclusive">0.123</param>
+ * <param name="maxExclusive">99.98</param>
+ * <message>Percentage needs to be between ${minExclusive} and ${maxExclusive} (exclusive)</message>
+ * </field-validator>
+ * </field>
+ *
+ * <!-- Field Validator Syntax with expression -->
+ * <field name="percentage">
+ * <field-validator type="double">
+ * <param name="parse">true</param>
+ * <param name="minExclusive">${minExclusiveValue}</param> <!-- will be evaluated as: Double getMinExclusiveValue() -->
+ * <param name="maxExclusive">${maxExclusive}</param> <!-- will be evaluated as: Double getMaxExclusive() -->
+ * <message>Percentage needs to be between ${minExclusive} and ${maxExclusive} (exclusive)</message>
+ * </field-validator>
+ * </field>
+ * </validators>
*
*
*
@@ -70,7 +80,6 @@ and ${maxExclusive} (exclusive)</message>
*
* @version $Id$
*/
-// START SNIPPET: field-level-validator
public class DoubleRangeFieldValidator extends FieldValidatorSupport {
String maxInclusive = null;
@@ -105,14 +114,22 @@ public class DoubleRangeFieldValidator extends FieldValidatorSupport {
}
}
- 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 extends FieldValidatorSupport {
public void setMaxExclusive(String maxExclusive) {
this.maxExclusive = maxExclusive;
}
+
}
-// END SNIPPET: field-level-validator
diff --git a/xwork-core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeValidatorTest.java b/xwork-core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeValidatorTest.java
index b160080f7..b11807f83 100644
--- a/xwork-core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeValidatorTest.java
+++ b/xwork-core/src/test/java/com/opensymphony/xwork2/validator/DoubleRangeValidatorTest.java
@@ -187,6 +187,35 @@ public class DoubleRangeValidatorTest extends XWorkTestCase {
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();