mirror of
https://github.com/apache/struts.git
synced 2026-08-07 15:46:57 +00:00
WW-4011 Improves ShortRangeFieldValidator annotation to match ShortRangeFieldValidator class
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1457733 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
+9
-2
@@ -680,12 +680,18 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
params.put("fieldName", v.fieldName());
|
||||
}
|
||||
|
||||
if (v.min() != null && v.min().length() > 0) {
|
||||
if (StringUtils.isNotEmpty(v.min())) {
|
||||
params.put("min", v.min());
|
||||
}
|
||||
if (v.max() != null && v.max().length() > 0) {
|
||||
if (StringUtils.isNotEmpty(v.max())) {
|
||||
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)
|
||||
@@ -694,6 +700,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>
|
||||
@@ -114,15 +120,25 @@ import java.lang.annotation.Target;
|
||||
public @interface ShortRangeFieldValidator {
|
||||
|
||||
/**
|
||||
* Integer property. The minimum the number must be.
|
||||
* Short property. The minimum the number must be.
|
||||
*/
|
||||
String min() default "";
|
||||
|
||||
/**
|
||||
* Integer property. The maximum number can be.
|
||||
* The minimum the number must be defined as an expression.
|
||||
*/
|
||||
String minExpression() default "";
|
||||
|
||||
/**
|
||||
* Short 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!
|
||||
@@ -134,6 +150,11 @@ public @interface ShortRangeFieldValidator {
|
||||
*/
|
||||
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
@@ -13,6 +13,7 @@ import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.ShortRangeFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.ValidationParameter;
|
||||
|
||||
/**
|
||||
@@ -51,6 +52,8 @@ public class AnnotationValidationAction extends ActionSupport {
|
||||
messageParams = {"one", "two", "three"}, shortCircuit = true)
|
||||
@RequiredStringValidator(fieldName = "foo", key = "requiredstring.key", message = "Foo is required!",
|
||||
messageParams = {"one", "two", "three"}, shortCircuit = true, trim = false)
|
||||
@ShortRangeFieldValidator(fieldName = "foo", key = "short.key", message = "Foo is out of range!", min = "1", max = "10",
|
||||
messageParams = {"one", "two", "three"}, shortCircuit = true)
|
||||
public String execute() {
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
+15
-2
@@ -25,6 +25,7 @@ import com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.validators.RegexFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.validators.RequiredFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.validators.RequiredStringValidator;
|
||||
import com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
@@ -46,7 +47,7 @@ public class AnnotationValidationConfigurationBuilderTest extends XWorkTestCase
|
||||
List<Validator> validators = manager.getValidators(AnnotationValidationAction.class, null);
|
||||
|
||||
// then
|
||||
assertEquals(validators.size(), 12);
|
||||
assertEquals(validators.size(), 13);
|
||||
for (Validator validator : validators) {
|
||||
validate(validator);
|
||||
}
|
||||
@@ -63,7 +64,7 @@ public class AnnotationValidationConfigurationBuilderTest extends XWorkTestCase
|
||||
ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
|
||||
valueStack.push(new AnnotationValidationExpAction());
|
||||
|
||||
assertEquals(validators.size(), 12);
|
||||
assertEquals(validators.size(), 13);
|
||||
for (Validator validator : validators) {
|
||||
validator.setValueStack(valueStack);
|
||||
validate(validator);
|
||||
@@ -95,9 +96,21 @@ public class AnnotationValidationConfigurationBuilderTest extends XWorkTestCase
|
||||
validateRequiredFieldValidator((RequiredFieldValidator) validator);
|
||||
} else if (validator.getValidatorType().equals("requiredstring")) {
|
||||
validateRequiredStringValidator((RequiredStringValidator) validator);
|
||||
}else if (validator.getValidatorType().equals("short")) {
|
||||
validateShortRangeFieldValidator((ShortRangeFieldValidator) validator);
|
||||
}
|
||||
}
|
||||
|
||||
private void validateShortRangeFieldValidator(ShortRangeFieldValidator validator) {
|
||||
assertEquals("foo", validator.getFieldName());
|
||||
assertEquals("Foo is out of range!", validator.getDefaultMessage());
|
||||
assertEquals("short.key", validator.getMessageKey());
|
||||
assertTrue(Arrays.equals(new String[]{"one", "two", "three"}, validator.getMessageParameters()));
|
||||
assertEquals(Short.valueOf("10"), validator.getMax());
|
||||
assertEquals(Short.valueOf("1"), validator.getMin());
|
||||
assertEquals(true, validator.isShortCircuit());
|
||||
}
|
||||
|
||||
private void validateRequiredStringValidator(RequiredStringValidator validator) {
|
||||
assertEquals("foo", validator.getFieldName());
|
||||
assertEquals("requiredstring.key", validator.getMessageKey());
|
||||
|
||||
+12
@@ -13,6 +13,7 @@ import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.ShortRangeFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.ValidationParameter;
|
||||
|
||||
import java.text.ParseException;
|
||||
@@ -57,6 +58,9 @@ public class AnnotationValidationExpAction extends ActionSupport {
|
||||
messageParams = {"one", "two", "three"}, shortCircuit = true)
|
||||
@RequiredStringValidator(fieldName = "foo", key = "requiredstring.key", message = "Foo is required!",
|
||||
messageParams = {"one", "two", "three"}, shortCircuit = true, trim = false)
|
||||
@ShortRangeFieldValidator(fieldName = "foo", key = "short.key", message = "Foo is out of range!",
|
||||
minExpression = "${shortMin}", maxExpression = "${shortMax}",
|
||||
messageParams = {"one", "two", "three"}, shortCircuit = true)
|
||||
public String execute() {
|
||||
return SUCCESS;
|
||||
}
|
||||
@@ -105,4 +109,12 @@ public class AnnotationValidationExpAction extends ActionSupport {
|
||||
return 1;
|
||||
}
|
||||
|
||||
public short getShortMin() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
public short getShortMax() {
|
||||
return 10;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user