mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
WW-4578 Converts required string validator to support collections
This commit is contained in:
+3
-3
@@ -25,9 +25,9 @@ import com.opensymphony.xwork2.validator.FieldValidator;
|
||||
*/
|
||||
public abstract class FieldValidatorSupport extends ValidatorSupport implements FieldValidator {
|
||||
|
||||
private String fieldName;
|
||||
private String type;
|
||||
private Object currentValue;
|
||||
protected String fieldName;
|
||||
protected String type;
|
||||
protected Object currentValue;
|
||||
|
||||
public void setFieldName(String fieldName) {
|
||||
this.fieldName = fieldName;
|
||||
|
||||
+34
-8
@@ -17,6 +17,7 @@ package com.opensymphony.xwork2.validator.validators;
|
||||
|
||||
import com.opensymphony.xwork2.validator.ValidationException;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* <!-- START SNIPPET: javadoc -->
|
||||
@@ -85,21 +86,46 @@ public class RequiredStringValidator extends FieldValidatorSupport {
|
||||
}
|
||||
|
||||
public void validate(Object object) throws ValidationException {
|
||||
String fieldName = getFieldName();
|
||||
Object value = this.getFieldValue(fieldName, object);
|
||||
Object fieldValue = this.getFieldValue(getFieldName(), object);
|
||||
|
||||
if (!(value instanceof String)) {
|
||||
addFieldError(fieldName, object);
|
||||
if (fieldValue == null) {
|
||||
addFieldError(getFieldName(), object);
|
||||
return;
|
||||
}
|
||||
|
||||
if (fieldValue.getClass().isArray()) {
|
||||
Object[] values = (Object[]) fieldValue;
|
||||
for (Object value : values) {
|
||||
validateValue(object, value);
|
||||
}
|
||||
} else if (Collection.class.isAssignableFrom(fieldValue.getClass())) {
|
||||
Collection values = (Collection) fieldValue;
|
||||
for (Object value : values) {
|
||||
validateValue(object, value);
|
||||
}
|
||||
} else {
|
||||
String s = (String) value;
|
||||
validateValue(object, fieldValue);
|
||||
}
|
||||
}
|
||||
|
||||
protected void validateValue(Object object, Object fieldValue) {
|
||||
if (fieldValue == null) {
|
||||
addFieldError(getFieldName(), object);
|
||||
return;
|
||||
}
|
||||
|
||||
if (fieldValue instanceof String) {
|
||||
String stingValue = (String) fieldValue;
|
||||
|
||||
if (trim) {
|
||||
s = s.trim();
|
||||
stingValue = stingValue.trim();
|
||||
}
|
||||
|
||||
if (s.length() == 0) {
|
||||
addFieldError(fieldName, object);
|
||||
if (stingValue.length() == 0) {
|
||||
addFieldError(getFieldName(), object);
|
||||
}
|
||||
} else {
|
||||
addFieldError(getFieldName(), object);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+48
@@ -8,6 +8,8 @@ import com.opensymphony.xwork2.util.ValueStack;
|
||||
import com.opensymphony.xwork2.validator.DummyValidatorContext;
|
||||
import com.opensymphony.xwork2.validator.ValidatorContext;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class RequiredStringValidatorTest extends XWorkTestCase {
|
||||
|
||||
private TextProviderFactory tpf;
|
||||
@@ -38,6 +40,52 @@ public class RequiredStringValidatorTest extends XWorkTestCase {
|
||||
assertTrue(context.getFieldErrors().size() == 0);
|
||||
}
|
||||
|
||||
public void testRequiredArrayOfStringsPass() throws Exception {
|
||||
// given
|
||||
ValueStack valueStack = ActionContext.getContext().getValueStack();
|
||||
|
||||
ValidationAction action = new ValidationAction();
|
||||
action.setStrings(new String[]{"", "12334", null});
|
||||
valueStack.push(action);
|
||||
|
||||
ValidatorContext context = new DummyValidatorContext(action, tpf);
|
||||
RequiredStringValidator validator = new RequiredStringValidator();
|
||||
validator.setValidatorContext(context);
|
||||
validator.setFieldName("strings");
|
||||
validator.setValueStack(valueStack);
|
||||
|
||||
// when
|
||||
validator.validate(action);
|
||||
|
||||
// then
|
||||
assertTrue(context.hasFieldErrors());
|
||||
assertEquals(1, context.getFieldErrors().size());
|
||||
assertEquals(2, context.getFieldErrors().get("strings").size());
|
||||
}
|
||||
|
||||
public void testRequiredCollectionOfStringsPass() throws Exception {
|
||||
// given
|
||||
ValueStack valueStack = ActionContext.getContext().getValueStack();
|
||||
|
||||
ValidationAction action = new ValidationAction();
|
||||
action.setStringCollection(Arrays.asList("", "123456", null));
|
||||
valueStack.push(action);
|
||||
|
||||
ValidatorContext context = new DummyValidatorContext(action, tpf);
|
||||
RequiredStringValidator validator = new RequiredStringValidator();
|
||||
validator.setValidatorContext(context);
|
||||
validator.setFieldName("stringCollection");
|
||||
validator.setValueStack(valueStack);
|
||||
|
||||
// when
|
||||
validator.validate(action);
|
||||
|
||||
// then
|
||||
assertTrue(context.hasFieldErrors());
|
||||
assertEquals(1, context.getFieldErrors().size());
|
||||
assertEquals(2, context.getFieldErrors().get("stringCollection").size());
|
||||
}
|
||||
|
||||
public void testRequiredStringFails() throws Exception {
|
||||
// given
|
||||
ValueStack valueStack = ActionContext.getContext().getValueStack();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.opensymphony.xwork2.validator.validators;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@@ -22,8 +23,11 @@ public class ValidationAction {
|
||||
private Date dateRange;
|
||||
private Date dateMinValue;
|
||||
private Date dateMaxValue;
|
||||
|
||||
private String dateFormat;
|
||||
private String stringValue;
|
||||
private String[] strings;
|
||||
private Collection<String> stringCollection;
|
||||
|
||||
public Integer getIntRange() {
|
||||
return intRange;
|
||||
@@ -152,4 +156,20 @@ public class ValidationAction {
|
||||
public void setShorts(List<Short> shorts) {
|
||||
this.shorts = shorts;
|
||||
}
|
||||
|
||||
public String[] getStrings() {
|
||||
return strings;
|
||||
}
|
||||
|
||||
public void setStrings(String[] strings) {
|
||||
this.strings = strings;
|
||||
}
|
||||
|
||||
public Collection<String> getStringCollection() {
|
||||
return stringCollection;
|
||||
}
|
||||
|
||||
public void setStringCollection(Collection<String> stringCollection) {
|
||||
this.stringCollection = stringCollection;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user