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 --> - * <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> + * <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> + * + * <!-- 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> * <!-- END SNIPPET: examples --> * </pre> * @@ -70,7 +80,6 @@ and ${maxExclusive} (exclusive)</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();