mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
WW-3955 adds support to specify maxInclusive, minInclusive, minExclusive and maxExclusive params as expression
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1428576 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
+54
-37
@@ -26,42 +26,52 @@ import com.opensymphony.xwork2.validator.ValidationException;
|
||||
*
|
||||
* <!-- 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>
|
||||
* <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>
|
||||
* <!-- END SNIPPET: examples -->
|
||||
* </pre>
|
||||
*
|
||||
@@ -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
|
||||
|
||||
+29
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user