mirror of
https://github.com/apache/struts.git
synced 2026-08-07 07:37:20 +00:00
WW-4008 Improves IntRangeFieldValidator annotation to match IntRangeFieldValidator class
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1457708 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
+7
@@ -649,6 +649,12 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
if (v.max() != null && v.max().length() > 0) {
|
||||
params.put("max", v.max());
|
||||
}
|
||||
if (StringUtils.isNotEmpty(v.maxExpression())) {
|
||||
params.put("maxExpression", v.maxExpression());
|
||||
}
|
||||
if (StringUtils.isNotEmpty(v.minExpression())) {
|
||||
params.put("minExpression", v.minExpression());
|
||||
}
|
||||
|
||||
validatorFactory.lookupRegisteredValidatorType(validatorType);
|
||||
return new ValidatorConfig.Builder(validatorType)
|
||||
@@ -657,6 +663,7 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
.shortCircuit(v.shortCircuit())
|
||||
.defaultMessage(v.message())
|
||||
.messageKey(v.key())
|
||||
.messageParams(v.messageParams())
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
+23
-2
@@ -56,6 +56,12 @@ import java.lang.annotation.Target;
|
||||
* <td class='confluenceTd'>i18n key from language specific properties file.</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td class='confluenceTd'>messageParams</td>
|
||||
* <td class='confluenceTd'>no</td>
|
||||
* <td class='confluenceTd'> </td>
|
||||
* <td class='confluenceTd'>Additional params to be used to customize message - will be evaluated against the Value Stack</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td class='confluenceTd'>fieldName</td>
|
||||
* <td class='confluenceTd'>no</td>
|
||||
* <td class='confluenceTd'> </td>
|
||||
@@ -104,7 +110,7 @@ import java.lang.annotation.Target;
|
||||
* <!-- START SNIPPET: example -->
|
||||
* @IntRangeFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true, min = "0", max = "42")
|
||||
*
|
||||
* @IntRangeFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true, min = "${minValue}", max = "${maxValue}" parse="true")
|
||||
* @IntRangeFieldValidator(message = "Default message", key = "i18n.key", shortCircuit = true, minExpression = "${minValue}", maxExpression = "${maxValue}")
|
||||
* <!-- END SNIPPET: example -->
|
||||
* </pre>
|
||||
*
|
||||
@@ -122,10 +128,20 @@ public @interface IntRangeFieldValidator {
|
||||
String min() default "";
|
||||
|
||||
/**
|
||||
* Integer property. The maximum number can be.
|
||||
* The minimum number can be defined as an expression
|
||||
*/
|
||||
String minExpression() default "";
|
||||
|
||||
/**
|
||||
* Integer property. The maximum number can be.
|
||||
*/
|
||||
String max() default "";
|
||||
|
||||
/**
|
||||
* The maximum number can be defined as an expression
|
||||
*/
|
||||
String maxExpression() default "";
|
||||
|
||||
/**
|
||||
* The default error message for this validator.
|
||||
* NOTE: It is required to set a message, if you are not using the message key for 18n lookup!
|
||||
@@ -137,6 +153,11 @@ public @interface IntRangeFieldValidator {
|
||||
*/
|
||||
String key() default "";
|
||||
|
||||
/**
|
||||
* Additional params to be used to customize message - will be evaluated against the Value Stack
|
||||
*/
|
||||
String[] messageParams() default {};
|
||||
|
||||
/**
|
||||
* The optional fieldName for SIMPLE validator types.
|
||||
*/
|
||||
|
||||
+3
@@ -9,6 +9,7 @@ import com.opensymphony.xwork2.validator.annotations.DoubleRangeFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.EmailValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.ExpressionValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.FieldExpressionValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.ValidationParameter;
|
||||
|
||||
@@ -42,6 +43,8 @@ public class AnnotationValidationAction extends ActionSupport {
|
||||
messageParams = {"one", "two", "three"}, shortCircuit = true)
|
||||
@FieldExpressionValidator(expression = "true", fieldName = "foo", key = "fieldexpression.key", message = "It is not true!",
|
||||
messageParams = {"one", "two", "three"}, shortCircuit = true)
|
||||
@IntRangeFieldValidator(fieldName = "foo", key = "int.key", message = "Foo is out of range!", max = "10", min = "1",
|
||||
messageParams = {"one", "two", "three"}, shortCircuit = true)
|
||||
public String execute() {
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
+15
-2
@@ -21,6 +21,7 @@ import com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.validators.EmailValidator;
|
||||
import com.opensymphony.xwork2.validator.validators.ExpressionValidator;
|
||||
import com.opensymphony.xwork2.validator.validators.FieldExpressionValidator;
|
||||
import com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.validators.RegexFieldValidator;
|
||||
|
||||
import java.text.ParseException;
|
||||
@@ -43,7 +44,7 @@ public class AnnotationValidationConfigurationBuilderTest extends XWorkTestCase
|
||||
List<Validator> validators = manager.getValidators(AnnotationValidationAction.class, null);
|
||||
|
||||
// then
|
||||
assertEquals(validators.size(), 9);
|
||||
assertEquals(validators.size(), 10);
|
||||
for (Validator validator : validators) {
|
||||
validate(validator);
|
||||
}
|
||||
@@ -60,7 +61,7 @@ public class AnnotationValidationConfigurationBuilderTest extends XWorkTestCase
|
||||
ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
|
||||
valueStack.push(new AnnotationValidationExpAction());
|
||||
|
||||
assertEquals(validators.size(), 9);
|
||||
assertEquals(validators.size(), 10);
|
||||
for (Validator validator : validators) {
|
||||
validator.setValueStack(valueStack);
|
||||
validate(validator);
|
||||
@@ -86,9 +87,21 @@ public class AnnotationValidationConfigurationBuilderTest extends XWorkTestCase
|
||||
validateExpressionValidator((ExpressionValidator) validator);
|
||||
} else if (validator.getValidatorType().equals("fieldexpression")) {
|
||||
validateFieldExpressionValidator((FieldExpressionValidator) validator);
|
||||
} else if (validator.getValidatorType().equals("int")) {
|
||||
validateIntRangeFieldValidator((IntRangeFieldValidator) validator);
|
||||
}
|
||||
}
|
||||
|
||||
private void validateIntRangeFieldValidator(IntRangeFieldValidator validator) {
|
||||
assertEquals("foo", validator.getFieldName());
|
||||
assertEquals("int.key", validator.getMessageKey());
|
||||
assertEquals("Foo is out of range!", validator.getDefaultMessage());
|
||||
assertTrue(Arrays.equals(new String[]{"one", "two", "three"}, validator.getMessageParameters()));
|
||||
assertEquals(true, validator.isShortCircuit());
|
||||
assertEquals(Integer.valueOf(10), validator.getMax());
|
||||
assertEquals(Integer.valueOf(1), validator.getMin());
|
||||
}
|
||||
|
||||
private void validateFieldExpressionValidator(FieldExpressionValidator validator) {
|
||||
assertEquals("foo", validator.getFieldName());
|
||||
assertEquals("It is not true!", validator.getDefaultMessage());
|
||||
|
||||
+12
@@ -9,6 +9,7 @@ import com.opensymphony.xwork2.validator.annotations.DoubleRangeFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.EmailValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.ExpressionValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.FieldExpressionValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.ValidationParameter;
|
||||
|
||||
@@ -47,6 +48,9 @@ public class AnnotationValidationExpAction extends ActionSupport {
|
||||
messageParams = {"one", "two", "three"}, shortCircuit = true)
|
||||
@FieldExpressionValidator(expression = "true", fieldName = "foo", key = "fieldexpression.key", message = "It is not true!",
|
||||
messageParams = {"one", "two", "three"}, shortCircuit = true)
|
||||
@IntRangeFieldValidator(fieldName = "foo", key = "int.key", message = "Foo is out of range!",
|
||||
maxExpression = "${intMax}", minExpression = "${intMin}",
|
||||
messageParams = {"one", "two", "three"}, shortCircuit = true)
|
||||
public String execute() {
|
||||
return SUCCESS;
|
||||
}
|
||||
@@ -87,4 +91,12 @@ public class AnnotationValidationExpAction extends ActionSupport {
|
||||
return 0.1;
|
||||
}
|
||||
|
||||
public int getIntMax() {
|
||||
return 10;
|
||||
}
|
||||
|
||||
public int getIntMin() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user