mirror of
https://github.com/apache/struts.git
synced 2026-08-11 09:36:57 +00:00
WW-3888 adds support for expression in specifying min and max constraints
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1397242 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -99,9 +99,9 @@ END SNIPPET: supported-validators
|
||||
<#elseif validator.validatorType = "int">
|
||||
if (continueValidation && field.value != null) {
|
||||
if (<#if validator.min??>parseInt(field.value) <
|
||||
${validator.min?c}<#else>false</#if> ||
|
||||
${validator.min}<#else>false</#if> ||
|
||||
<#if validator.max??>parseInt(field.value) >
|
||||
${validator.max?c}<#else>false</#if>) {
|
||||
${validator.max}<#else>false</#if>) {
|
||||
addError(field, error);
|
||||
errors = true;
|
||||
<#if validator.shortCircuit>continueValidation = false;</#if>
|
||||
|
||||
+17
-6
@@ -24,11 +24,11 @@ import com.opensymphony.xwork2.validator.ValidationException;
|
||||
* @author Jason Carreira
|
||||
* @author Cameron Braid
|
||||
*/
|
||||
public abstract class AbstractRangeValidator extends FieldValidatorSupport {
|
||||
public abstract class AbstractRangeValidator<T extends Comparable> extends FieldValidatorSupport {
|
||||
|
||||
public void validate(Object object) throws ValidationException {
|
||||
Object obj = getFieldValue(getFieldName(), object);
|
||||
Comparable value = (Comparable) obj;
|
||||
Comparable<T> value = (Comparable<T>) obj;
|
||||
|
||||
// if there is no value - don't do comparison
|
||||
// if a value is required, a required validator should be added to the field
|
||||
@@ -37,17 +37,28 @@ public abstract class AbstractRangeValidator extends FieldValidatorSupport {
|
||||
}
|
||||
|
||||
// only check for a minimum value if the min parameter is set
|
||||
if ((getMinComparatorValue() != null) && (value.compareTo(getMinComparatorValue()) < 0)) {
|
||||
T minComparatorValue = getMinComparatorValue();
|
||||
if ((minComparatorValue != null) && (value.compareTo(minComparatorValue) < 0)) {
|
||||
addFieldError(getFieldName(), object);
|
||||
}
|
||||
|
||||
// only check for a maximum value if the max parameter is set
|
||||
if ((getMaxComparatorValue() != null) && (value.compareTo(getMaxComparatorValue()) > 0)) {
|
||||
T maxComparatorValue = getMaxComparatorValue();
|
||||
if ((maxComparatorValue != null) && (value.compareTo(maxComparatorValue) > 0)) {
|
||||
addFieldError(getFieldName(), object);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract Comparable getMaxComparatorValue();
|
||||
protected abstract T getMaxComparatorValue();
|
||||
|
||||
protected abstract Comparable getMinComparatorValue();
|
||||
protected abstract T getMinComparatorValue();
|
||||
|
||||
protected String safeConditionalParse(String expression) {
|
||||
Object value = conditionalParse(expression);
|
||||
if (value != null) {
|
||||
return value.toString();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+22
-14
@@ -59,35 +59,43 @@ package com.opensymphony.xwork2.validator.validators;
|
||||
* @author Jason Carreira
|
||||
* @version $Date$ $Id$
|
||||
*/
|
||||
public class IntRangeFieldValidator extends AbstractRangeValidator {
|
||||
public class IntRangeFieldValidator extends AbstractRangeValidator<Integer> {
|
||||
|
||||
Integer max = null;
|
||||
Integer min = null;
|
||||
String max = null;
|
||||
String min = null;
|
||||
|
||||
|
||||
public void setMax(Integer max) {
|
||||
public void setMax(String max) {
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
public Integer getMax() {
|
||||
return max;
|
||||
public String getMax() {
|
||||
return safeConditionalParse(max);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Comparable getMaxComparatorValue() {
|
||||
return max;
|
||||
public Integer getMaxComparatorValue() {
|
||||
return parseInt(getMax());
|
||||
}
|
||||
|
||||
public void setMin(Integer min) {
|
||||
public void setMin(String min) {
|
||||
this.min = min;
|
||||
}
|
||||
|
||||
public Integer getMin() {
|
||||
return min;
|
||||
public String getMin() {
|
||||
return safeConditionalParse(min);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Comparable getMinComparatorValue() {
|
||||
return min;
|
||||
public Integer getMinComparatorValue() {
|
||||
return parseInt(getMin());
|
||||
}
|
||||
|
||||
private Integer parseInt(String value) {
|
||||
if (value != null) {
|
||||
return Integer.parseInt(value);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+74
@@ -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 IntRangeFieldValidatorTest extends XWorkTestCase {
|
||||
|
||||
public void testPassValidation() throws Exception {
|
||||
// given
|
||||
ValidationAction action = prepareAction(100);
|
||||
ValidatorContext context = new GenericValidatorContext(action);
|
||||
IntRangeFieldValidator validator = prepareValidator(action, context);
|
||||
|
||||
// when
|
||||
validator.validate(action);
|
||||
|
||||
// then
|
||||
assertTrue(context.getFieldErrors().size() == 0);
|
||||
}
|
||||
|
||||
public void testMinValidation() throws Exception {
|
||||
// given
|
||||
ValidationAction action = prepareAction(98);
|
||||
ValidatorContext context = new GenericValidatorContext(action);
|
||||
IntRangeFieldValidator validator = prepareValidator(action, context);
|
||||
|
||||
// when
|
||||
validator.validate(action);
|
||||
|
||||
// then
|
||||
assertTrue(context.getFieldErrors().size() == 1);
|
||||
assertEquals("Max is 101, min is 99 but value is 98", context.getFieldErrors().get("intRange").get(0));
|
||||
}
|
||||
|
||||
public void testMaxValidation() throws Exception {
|
||||
// given
|
||||
ValidationAction action = prepareAction(102);
|
||||
ValidatorContext context = new GenericValidatorContext(action);
|
||||
IntRangeFieldValidator validator = prepareValidator(action, context);
|
||||
|
||||
// when
|
||||
validator.validate(action);
|
||||
|
||||
// then
|
||||
assertTrue(context.getFieldErrors().size() == 1);
|
||||
assertEquals("Max is 101, min is 99 but value is 102", context.getFieldErrors().get("intRange").get(0));
|
||||
}
|
||||
|
||||
private ValidationAction prepareAction(int intRange) {
|
||||
ValidationAction action = new ValidationAction();
|
||||
action.setMaxValue(101);
|
||||
action.setMinValue(99);
|
||||
action.setIntRange(intRange);
|
||||
return action;
|
||||
}
|
||||
|
||||
private IntRangeFieldValidator prepareValidator(ValidationAction action, ValidatorContext context) {
|
||||
IntRangeFieldValidator validator = new IntRangeFieldValidator();
|
||||
validator.setMax("${maxValue}");
|
||||
validator.setMin("${minValue}");
|
||||
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}");
|
||||
return validator;
|
||||
}
|
||||
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.opensymphony.xwork2.validator.validators;
|
||||
|
||||
public class ValidationAction {
|
||||
|
||||
private Integer intRange;
|
||||
private Integer minValue;
|
||||
private Integer maxValue;
|
||||
|
||||
public Integer getIntRange() {
|
||||
return intRange;
|
||||
}
|
||||
|
||||
public void setIntRange(Integer intRange) {
|
||||
this.intRange = intRange;
|
||||
}
|
||||
|
||||
public Integer getMinValue() {
|
||||
return minValue;
|
||||
}
|
||||
|
||||
public void setMinValue(Integer minValue) {
|
||||
this.minValue = minValue;
|
||||
}
|
||||
|
||||
public Integer getMaxValue() {
|
||||
return maxValue;
|
||||
}
|
||||
|
||||
public void setMaxValue(Integer maxValue) {
|
||||
this.maxValue = maxValue;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user