mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
WW-4012 Improves StringLengthFieldValidator annotation to match StringLengthFieldValidator class
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1457742 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
+15
-5
@@ -545,18 +545,27 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
|
||||
if (fieldName != null) {
|
||||
params.put("fieldName", fieldName);
|
||||
} else if (v.fieldName() != null && v.fieldName().length() > 0) {
|
||||
} else if (StringUtils.isNotEmpty(v.fieldName())) {
|
||||
params.put("fieldName", v.fieldName());
|
||||
}
|
||||
|
||||
if (v.maxLength() != null && v.maxLength().length() > 0) {
|
||||
if (StringUtils.isNotEmpty(v.maxLength())) {
|
||||
params.put("maxLength", v.maxLength());
|
||||
}
|
||||
if (v.minLength() != null && v.minLength().length() > 0) {
|
||||
if (StringUtils.isNotEmpty(v.minLength())) {
|
||||
params.put("minLength", v.minLength());
|
||||
}
|
||||
params.put("trim", String.valueOf(v.trim()));
|
||||
|
||||
if (StringUtils.isNotEmpty(v.maxLengthExpression())) {
|
||||
params.put("maxLengthExpression", v.maxLengthExpression());
|
||||
}
|
||||
if (StringUtils.isNotEmpty(v.minLengthExpression())) {
|
||||
params.put("minLengthExpression", v.minLengthExpression());
|
||||
}
|
||||
if (StringUtils.isNotEmpty(v.trimExpression())){
|
||||
params.put("trimExpression", v.trimExpression());
|
||||
} else {
|
||||
params.put("trim", String.valueOf(v.trim()));
|
||||
}
|
||||
validatorFactory.lookupRegisteredValidatorType(validatorType);
|
||||
return new ValidatorConfig.Builder(validatorType)
|
||||
.addParams(params)
|
||||
@@ -564,6 +573,7 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
.shortCircuit(v.shortCircuit())
|
||||
.defaultMessage(v.message())
|
||||
.messageKey(v.key())
|
||||
.messageParams(v.messageParams())
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
+26
@@ -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>
|
||||
@@ -117,16 +123,31 @@ public @interface StringLengthFieldValidator {
|
||||
*/
|
||||
boolean trim() default true;
|
||||
|
||||
/**
|
||||
* Determines whether the String is trimmed before performing the length check but defined as an expression
|
||||
*/
|
||||
String trimExpression() default "";
|
||||
|
||||
/**
|
||||
* Integer property. The minimum length the String must be.
|
||||
*/
|
||||
String minLength() default "";
|
||||
|
||||
/**
|
||||
* The minimum length the String must be defined as an expression
|
||||
*/
|
||||
String minLengthExpression() default "";
|
||||
|
||||
/**
|
||||
* Integer property. The maximum length the String can be.
|
||||
*/
|
||||
String maxLength() default "";
|
||||
|
||||
/**
|
||||
* The maximum length the String can be defined as an expression
|
||||
*/
|
||||
String maxLengthExpression() 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!
|
||||
@@ -138,6 +159,11 @@ public @interface StringLengthFieldValidator {
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
|
||||
+40
-20
@@ -16,6 +16,7 @@
|
||||
package com.opensymphony.xwork2.validator.validators;
|
||||
|
||||
import com.opensymphony.xwork2.validator.ValidationException;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* <!-- START SNIPPET: javadoc -->
|
||||
@@ -26,24 +27,24 @@ import com.opensymphony.xwork2.validator.ValidationException;
|
||||
* String before performing the length check. If unspecified, the String will be trimmed.
|
||||
* <!-- END SNIPPET: javadoc -->
|
||||
* <p/>
|
||||
*
|
||||
*
|
||||
* <p/>
|
||||
* <p/>
|
||||
* <!-- START SNIPPET: parameters -->
|
||||
* <ul>
|
||||
* <li>fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required</li>
|
||||
* <li>maxLength - Integer. The max length of the field value. Default ignore.</li>
|
||||
* <li>minLength - Integer. The min length of the field value. Default ignore.</li>
|
||||
* <li>trim - (Optional) Boolean, default true. Trim the field value before evaluating its min/max length. Default true.</li>
|
||||
* <li>maxLengthExpression - (Optional) String. Defines the max length param as an OGNL expression</li>
|
||||
* <li>minLengthExpression - (Optional) String. Defines the min length param as an OGNL expression</li>
|
||||
* <li>trimExpression - (Optional) String. Defines th trim param as an OGNL expression</li>
|
||||
* <li>fieldName - The field name this validator is validating. Required if using Plain-Validator Syntax otherwise not required</li>
|
||||
* <li>maxLength - Integer. The max length of the field value. Default ignore.</li>
|
||||
* <li>minLength - Integer. The min length of the field value. Default ignore.</li>
|
||||
* <li>trim - (Optional) Boolean, default true. Trim the field value before evaluating its min/max length. Default true.</li>
|
||||
* <li>maxLengthExpression - (Optional) String. Defines the max length param as an OGNL expression</li>
|
||||
* <li>minLengthExpression - (Optional) String. Defines the min length param as an OGNL expression</li>
|
||||
* <li>trimExpression - (Optional) String. Defines th trim param as an OGNL expression</li>
|
||||
* </ul>
|
||||
* <!-- END SNIPPET: parameters -->
|
||||
*
|
||||
* <p/>
|
||||
* <!-- START SNIPPET: parameters-warning -->
|
||||
* Do not use ${minLengthExpression}, ${maxLengthExpression} and ${trimExpression} as an expression as this will turn into infinitive loop!
|
||||
* <!-- END SNIPPET: parameters-warning -->
|
||||
*
|
||||
* <p/>
|
||||
* <pre>
|
||||
* <!--START SNIPPET: example -->
|
||||
* <validators>
|
||||
@@ -78,7 +79,6 @@ import com.opensymphony.xwork2.validator.ValidationException;
|
||||
* </validators>
|
||||
* <!-- END SNIPPET: example -->
|
||||
* </pre>
|
||||
*
|
||||
*
|
||||
* @author Jason Carreira
|
||||
* @author Mark Woon
|
||||
@@ -91,16 +91,25 @@ public class StringLengthFieldValidator extends FieldValidatorSupport {
|
||||
private int maxLength = -1;
|
||||
private int minLength = -1;
|
||||
|
||||
private String maxLengthExpression;
|
||||
private String minLengthExpression;
|
||||
private String trimExpression;
|
||||
|
||||
public void setMaxLength(int maxLength) {
|
||||
this.maxLength = maxLength;
|
||||
}
|
||||
|
||||
public void setMaxLengthExpression(String maxLengthExpression) {
|
||||
this.maxLength = (Integer) parse(maxLengthExpression, Integer.class);
|
||||
this.maxLengthExpression = maxLengthExpression;
|
||||
}
|
||||
|
||||
public int getMaxLength() {
|
||||
return maxLength;
|
||||
if (maxLength > -1) {
|
||||
return maxLength;
|
||||
} else if (StringUtils.isNotEmpty(maxLengthExpression)) {
|
||||
return (Integer) parse(maxLengthExpression, Integer.class);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public void setMinLength(int minLength) {
|
||||
@@ -108,11 +117,16 @@ public class StringLengthFieldValidator extends FieldValidatorSupport {
|
||||
}
|
||||
|
||||
public void setMinLengthExpression(String minLengthExpression) {
|
||||
this.minLength = (Integer) parse(minLengthExpression, Integer.class);
|
||||
this.minLengthExpression = minLengthExpression;
|
||||
}
|
||||
|
||||
public int getMinLength() {
|
||||
return minLength;
|
||||
if (minLength > -1) {
|
||||
return minLength;
|
||||
} else if (StringUtils.isNotEmpty(minLengthExpression)) {
|
||||
return (Integer) parse(minLengthExpression, Integer.class);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public void setTrim(boolean trim) {
|
||||
@@ -120,10 +134,13 @@ public class StringLengthFieldValidator extends FieldValidatorSupport {
|
||||
}
|
||||
|
||||
public void setTrimExpression(String trimExpression) {
|
||||
this.trim = (Boolean) parse(trimExpression, Boolean.class);
|
||||
this.trimExpression = trimExpression;
|
||||
}
|
||||
|
||||
public boolean isTrim() {
|
||||
if (StringUtils.isNotEmpty(trimExpression)) {
|
||||
return (Boolean) parse(trimExpression, Boolean.class);
|
||||
}
|
||||
return trim;
|
||||
}
|
||||
|
||||
@@ -135,7 +152,7 @@ public class StringLengthFieldValidator extends FieldValidatorSupport {
|
||||
// use a required validator for these
|
||||
return;
|
||||
}
|
||||
if (trim) {
|
||||
if (isTrim()) {
|
||||
val = val.trim();
|
||||
if (val.length() <= 0) {
|
||||
// use a required validator
|
||||
@@ -143,9 +160,12 @@ public class StringLengthFieldValidator extends FieldValidatorSupport {
|
||||
}
|
||||
}
|
||||
|
||||
if ((minLength > -1) && (val.length() < minLength)) {
|
||||
int minLengthToUse = getMinLength();
|
||||
int maxLengthToUse = getMaxLength();
|
||||
|
||||
if ((minLengthToUse > -1) && (val.length() < minLengthToUse)) {
|
||||
addFieldError(fieldName, object);
|
||||
} else if ((maxLength > -1) && (val.length() > maxLength)) {
|
||||
} else if ((maxLengthToUse > -1) && (val.length() > maxLengthToUse)) {
|
||||
addFieldError(fieldName, object);
|
||||
}
|
||||
}
|
||||
|
||||
+3
@@ -14,6 +14,7 @@ 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.StringLengthFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.ValidationParameter;
|
||||
|
||||
/**
|
||||
@@ -54,6 +55,8 @@ public class AnnotationValidationAction extends ActionSupport {
|
||||
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)
|
||||
@StringLengthFieldValidator(fieldName = "foo", key = "stringlength.key", message = "Foo is too long!",
|
||||
maxLength = "10", minLength = "1", shortCircuit = true, trim = false, messageParams = {"one", "two", "three"})
|
||||
public String execute() {
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
+17
-3
@@ -26,6 +26,7 @@ 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 com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
@@ -47,7 +48,7 @@ public class AnnotationValidationConfigurationBuilderTest extends XWorkTestCase
|
||||
List<Validator> validators = manager.getValidators(AnnotationValidationAction.class, null);
|
||||
|
||||
// then
|
||||
assertEquals(validators.size(), 13);
|
||||
assertEquals(validators.size(), 14);
|
||||
for (Validator validator : validators) {
|
||||
validate(validator);
|
||||
}
|
||||
@@ -64,7 +65,7 @@ public class AnnotationValidationConfigurationBuilderTest extends XWorkTestCase
|
||||
ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
|
||||
valueStack.push(new AnnotationValidationExpAction());
|
||||
|
||||
assertEquals(validators.size(), 13);
|
||||
assertEquals(validators.size(), 14);
|
||||
for (Validator validator : validators) {
|
||||
validator.setValueStack(valueStack);
|
||||
validate(validator);
|
||||
@@ -96,11 +97,24 @@ public class AnnotationValidationConfigurationBuilderTest extends XWorkTestCase
|
||||
validateRequiredFieldValidator((RequiredFieldValidator) validator);
|
||||
} else if (validator.getValidatorType().equals("requiredstring")) {
|
||||
validateRequiredStringValidator((RequiredStringValidator) validator);
|
||||
}else if (validator.getValidatorType().equals("short")) {
|
||||
} else if (validator.getValidatorType().equals("short")) {
|
||||
validateShortRangeFieldValidator((ShortRangeFieldValidator) validator);
|
||||
} else if (validator.getValidatorType().equals("stringlength")) {
|
||||
validateStringLengthFieldValidator((StringLengthFieldValidator) validator);
|
||||
}
|
||||
}
|
||||
|
||||
private void validateStringLengthFieldValidator(StringLengthFieldValidator validator) {
|
||||
assertEquals("foo", validator.getFieldName());
|
||||
assertEquals("stringlength.key", validator.getMessageKey());
|
||||
assertEquals("Foo is too long!", validator.getDefaultMessage());
|
||||
assertTrue(Arrays.equals(new String[]{"one", "two", "three"}, validator.getMessageParameters()));
|
||||
assertEquals(1, validator.getMinLength());
|
||||
assertEquals(10, validator.getMaxLength());
|
||||
assertEquals(true, validator.isShortCircuit());
|
||||
assertEquals(false, validator.isTrim());
|
||||
}
|
||||
|
||||
private void validateShortRangeFieldValidator(ShortRangeFieldValidator validator) {
|
||||
assertEquals("foo", validator.getFieldName());
|
||||
assertEquals("Foo is out of range!", validator.getDefaultMessage());
|
||||
|
||||
+13
@@ -14,6 +14,7 @@ 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.StringLengthFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.ValidationParameter;
|
||||
|
||||
import java.text.ParseException;
|
||||
@@ -61,6 +62,9 @@ public class AnnotationValidationExpAction extends ActionSupport {
|
||||
@ShortRangeFieldValidator(fieldName = "foo", key = "short.key", message = "Foo is out of range!",
|
||||
minExpression = "${shortMin}", maxExpression = "${shortMax}",
|
||||
messageParams = {"one", "two", "three"}, shortCircuit = true)
|
||||
@StringLengthFieldValidator(fieldName = "foo", key = "stringlength.key", message = "Foo is too long!",
|
||||
maxLengthExpression = "${maxLength}", minLengthExpression = "${minLength}", shortCircuit = true, trimExpression = "${false}",
|
||||
messageParams = {"one", "two", "three"})
|
||||
public String execute() {
|
||||
return SUCCESS;
|
||||
}
|
||||
@@ -117,4 +121,13 @@ public class AnnotationValidationExpAction extends ActionSupport {
|
||||
return 10;
|
||||
}
|
||||
|
||||
public int getMaxLength() {
|
||||
return 10;
|
||||
}
|
||||
|
||||
public int getMinLength() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user