mirror of
https://github.com/apache/struts.git
synced 2026-08-07 15:46:57 +00:00
WW-4002 Improves CustomValidator annotation
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1457442 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
+1
@@ -423,6 +423,7 @@ public class AnnotationValidationConfigurationBuilder {
|
||||
.shortCircuit(v.shortCircuit())
|
||||
.defaultMessage(v.message())
|
||||
.messageKey(v.key())
|
||||
.messageParams(v.messageParams())
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
+13
-2
@@ -55,6 +55,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>
|
||||
@@ -69,8 +75,8 @@ import java.lang.annotation.Target;
|
||||
* <tr>
|
||||
* <td class='confluenceTd'>type</td>
|
||||
* <td class='confluenceTd'>yes</td>
|
||||
* <td class='confluenceTd'>ValidatorType.FIELD</td>
|
||||
* <td class='confluenceTd'>Enum value from ValidatorType. Either FIELD or SIMPLE can be used here.</td>
|
||||
* <td class='confluenceTd'>name of validator</td>
|
||||
* <td class='confluenceTd'>Simple string which identifies that validator among other</td>
|
||||
* </tr>
|
||||
* </table>
|
||||
* <!-- END SNIPPET: parameters -->
|
||||
@@ -106,6 +112,11 @@ public @interface CustomValidator {
|
||||
|
||||
String key() default "";
|
||||
|
||||
/**
|
||||
* Additional params to be used to customize message - will be evaluated against the Value Stack
|
||||
*/
|
||||
String[] messageParams() default {};
|
||||
|
||||
public ValidationParameter[] parameters() default {};
|
||||
|
||||
boolean shortCircuit() default false;
|
||||
|
||||
+8
@@ -3,7 +3,9 @@ package com.opensymphony.xwork2.validator;
|
||||
import com.opensymphony.xwork2.ActionSupport;
|
||||
import com.opensymphony.xwork2.validator.annotations.ConditionalVisitorFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.ConversionErrorFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.CustomValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.ValidationParameter;
|
||||
|
||||
/**
|
||||
* Sets up all available validation annotations
|
||||
@@ -18,6 +20,12 @@ public class AnnotationValidationAction extends ActionSupport {
|
||||
messageParams = {"one", "two", "three"})
|
||||
@ConversionErrorFieldValidator(fieldName = "bar", key = "conversion.key", message = "Foo conversion error!",
|
||||
shortCircuit = true, repopulateField = true, messageParams = {"one", "three"})
|
||||
@CustomValidator(type = "myValidator", fieldName = "foo", key = "foo.invalid", message = "Foo is invalid!",
|
||||
shortCircuit = true, messageParams = {"one", "two", "three"},
|
||||
parameters = {
|
||||
@ValidationParameter(name = "value", value = "1")
|
||||
}
|
||||
)
|
||||
public String execute() {
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
+16
-2
@@ -36,7 +36,7 @@ public class AnnotationValidationConfigurationBuilderTest extends XWorkTestCase
|
||||
List<Validator> validators = manager.getValidators(AnnotationValidationAction.class, null);
|
||||
|
||||
// then
|
||||
assertEquals(validators.size(), 3);
|
||||
assertEquals(validators.size(), 4);
|
||||
for (Validator validator : validators) {
|
||||
validate(validator);
|
||||
}
|
||||
@@ -53,7 +53,7 @@ public class AnnotationValidationConfigurationBuilderTest extends XWorkTestCase
|
||||
ValueStack valueStack = container.getInstance(ValueStackFactory.class).createValueStack();
|
||||
valueStack.push(new AnnotationValidationExpAction());
|
||||
|
||||
assertEquals(validators.size(), 3);
|
||||
assertEquals(validators.size(), 4);
|
||||
for (Validator validator : validators) {
|
||||
validator.setValueStack(valueStack);
|
||||
validate(validator);
|
||||
@@ -67,9 +67,20 @@ public class AnnotationValidationConfigurationBuilderTest extends XWorkTestCase
|
||||
validateConditionalFieldVisitorValidator((ConditionalVisitorFieldValidator) validator);
|
||||
} else if (validator.getValidatorType().equals("conversion")) {
|
||||
validateConversionFieldErrorVisitorValidator((ConversionErrorFieldValidator) validator);
|
||||
} else if (validator.getValidatorType().equals("myValidator")) {
|
||||
validateMyValidator((MyValidator) validator);
|
||||
}
|
||||
}
|
||||
|
||||
private void validateMyValidator(MyValidator validator) {
|
||||
assertEquals("Foo is invalid!", validator.getDefaultMessage());
|
||||
assertEquals("foo", validator.getFieldName());
|
||||
assertEquals("foo.invalid", validator.getMessageKey());
|
||||
assertTrue(Arrays.equals(new String[]{"one", "two", "three"}, validator.getMessageParameters()));
|
||||
assertEquals(true, validator.isShortCircuit());
|
||||
assertEquals(1, validator.getValue());
|
||||
}
|
||||
|
||||
private void validateConversionFieldErrorVisitorValidator(ConversionErrorFieldValidator validator) {
|
||||
assertEquals("bar", validator.getFieldName());
|
||||
assertEquals("conversion.key", validator.getMessageKey());
|
||||
@@ -134,6 +145,9 @@ public class AnnotationValidationConfigurationBuilderTest extends XWorkTestCase
|
||||
AnnotationActionValidatorManager manager = new AnnotationActionValidatorManager();
|
||||
container.inject(manager);
|
||||
|
||||
ValidatorFactory vf = container.getInstance(ValidatorFactory.class);
|
||||
vf.registerValidator("myValidator", MyValidator.class.getName());
|
||||
|
||||
return manager;
|
||||
}
|
||||
|
||||
|
||||
+8
@@ -3,7 +3,9 @@ package com.opensymphony.xwork2.validator;
|
||||
import com.opensymphony.xwork2.ActionSupport;
|
||||
import com.opensymphony.xwork2.validator.annotations.ConditionalVisitorFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.ConversionErrorFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.CustomValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
|
||||
import com.opensymphony.xwork2.validator.annotations.ValidationParameter;
|
||||
|
||||
/**
|
||||
* Sets up all available validation annotations with params as expressions
|
||||
@@ -18,6 +20,12 @@ public class AnnotationValidationExpAction extends ActionSupport {
|
||||
messageParams = {"one", "two", "three"})
|
||||
@ConversionErrorFieldValidator(fieldName = "bar", key = "conversion.key", message = "Foo conversion error!",
|
||||
shortCircuit = true, repopulateField = true, messageParams = {"one", "three"})
|
||||
@CustomValidator(type = "myValidator", fieldName = "foo", key = "foo.invalid", message = "Foo is invalid!",
|
||||
shortCircuit = true, messageParams = {"one", "two", "three"},
|
||||
parameters = {
|
||||
@ValidationParameter(name = "value", value = "1")
|
||||
}
|
||||
)
|
||||
public String execute() {
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package com.opensymphony.xwork2.validator;
|
||||
|
||||
import com.opensymphony.xwork2.util.ValueStack;
|
||||
|
||||
/**
|
||||
* TODO lukaszlenart: write a JavaDoc
|
||||
*/
|
||||
public class MyValidator implements FieldValidator, ShortCircuitableValidator {
|
||||
|
||||
private String message;
|
||||
private String fieldName;
|
||||
private String key;
|
||||
private String[] messageParameters;
|
||||
private ValidatorContext validatorContext;
|
||||
private String type;
|
||||
private ValueStack stack;
|
||||
private boolean shortcircuit;
|
||||
|
||||
private int value;
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public void setFieldName(String fieldName) {
|
||||
this.fieldName = fieldName;
|
||||
}
|
||||
|
||||
public String getFieldName() {
|
||||
return fieldName;
|
||||
}
|
||||
|
||||
public void setDefaultMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getDefaultMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public String getMessage(Object object) {
|
||||
return "Message";
|
||||
}
|
||||
|
||||
public void setMessageKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public String getMessageKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setMessageParameters(String[] messageParameters) {
|
||||
this.messageParameters = messageParameters;
|
||||
}
|
||||
|
||||
public String[] getMessageParameters() {
|
||||
return messageParameters;
|
||||
}
|
||||
|
||||
public void setValidatorContext(ValidatorContext validatorContext) {
|
||||
this.validatorContext = validatorContext;
|
||||
}
|
||||
|
||||
public ValidatorContext getValidatorContext() {
|
||||
return validatorContext;
|
||||
}
|
||||
|
||||
public void validate(Object object) throws ValidationException {
|
||||
// pass
|
||||
}
|
||||
|
||||
public void setValidatorType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getValidatorType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setValueStack(ValueStack stack) {
|
||||
this.stack = stack;
|
||||
}
|
||||
|
||||
public void setShortCircuit(boolean shortcircuit) {
|
||||
this.shortcircuit = shortcircuit;
|
||||
}
|
||||
|
||||
public boolean isShortCircuit() {
|
||||
return shortcircuit;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user