diff --git a/plugins/oval/src/main/java/org/apache/struts2/oval/interceptor/OValValidationInterceptor.java b/plugins/oval/src/main/java/org/apache/struts2/oval/interceptor/OValValidationInterceptor.java index 904e9f074..176564282 100644 --- a/plugins/oval/src/main/java/org/apache/struts2/oval/interceptor/OValValidationInterceptor.java +++ b/plugins/oval/src/main/java/org/apache/struts2/oval/interceptor/OValValidationInterceptor.java @@ -180,13 +180,16 @@ public class OValValidationInterceptor extends MethodFilterInterceptor { //translate message String key = violation.getMessage(); + String message = key; + // push context variable into stack, to allow use ${max}, ${min} etc in error messages + valueStack.push(violation.getMessageVariables()); //push the validator into the stack valueStack.push(violation.getContext()); - String message = key; try { message = validatorContext.getText(key); } finally { valueStack.pop(); + valueStack.pop(); } if (isActionError(violation)) { diff --git a/plugins/oval/src/test/java/org/apache/struts2/oval/interceptor/OValValidationInterceptorTest.java b/plugins/oval/src/test/java/org/apache/struts2/oval/interceptor/OValValidationInterceptorTest.java index d521b995b..1e85d7c16 100644 --- a/plugins/oval/src/test/java/org/apache/struts2/oval/interceptor/OValValidationInterceptorTest.java +++ b/plugins/oval/src/test/java/org/apache/struts2/oval/interceptor/OValValidationInterceptorTest.java @@ -24,14 +24,12 @@ import com.opensymphony.xwork2.ActionProxy; import com.opensymphony.xwork2.ValidationAware; import com.opensymphony.xwork2.XWorkTestCase; import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider; +import net.sf.oval.configuration.Configurer; import java.util.Arrays; import java.util.List; -import java.util.Map; import java.util.Locale; - -import net.sf.oval.configuration.Configurer; -import org.apache.struts2.oval.interceptor.OValValidationManager; +import java.util.Map; public class OValValidationInterceptorTest extends XWorkTestCase { public void testSimpleFieldsXML() throws Exception { @@ -126,6 +124,20 @@ public class OValValidationInterceptorTest extends XWorkTestCase { assertEquals(0, fieldErrors.size()); } + public void testSimpleFieldTooLong() throws Exception { + ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "simpleFieldTooLong", null, null); + SimpleField action = (SimpleField) baseActionProxy.getAction(); + action.setName("12367"); + baseActionProxy.execute(); + + Map> fieldErrors = action.getFieldErrors(); + + assertNotNull(fieldErrors); + assertEquals(1, fieldErrors.size()); + assertValue(fieldErrors, "name", Arrays.asList("name is not between 0 and 3 characters long")); + assertValue(fieldErrors, "name", Arrays.asList("name is not between 0 and 3 characters long")); + } + public void testSimpleFieldMultipleValidators() throws Exception { ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "simpleField", null, null); SimpleField action = (SimpleField) baseActionProxy.getAction(); @@ -195,6 +207,19 @@ public class OValValidationInterceptorTest extends XWorkTestCase { assertValue(fieldErrors, "name", Arrays.asList("name cannot be null")); } + public void testSimpleFieldI18n2() throws Exception { + ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "simpleFieldI18n", null, null); + SimpleFieldI18n action = (SimpleFieldI18n) baseActionProxy.getAction(); + action.setName("123123"); + baseActionProxy.execute(); + + Map> fieldErrors = action.getFieldErrors(); + + assertNotNull(fieldErrors); + assertEquals(1, fieldErrors.size()); + assertValue(fieldErrors, "name", Arrays.asList("name value is too long, allowed length is 3")); + } + public void testSimpleFieldI18nDefaultKey() throws Exception { ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("oval", "simpleFieldI18nDefaultKey", null, null); SimpleFieldI18nDefaultKey action = (SimpleFieldI18nDefaultKey) baseActionProxy.getAction(); diff --git a/plugins/oval/src/test/java/org/apache/struts2/oval/interceptor/SimpleFieldI18n.java b/plugins/oval/src/test/java/org/apache/struts2/oval/interceptor/SimpleFieldI18n.java index d5143fc27..799732c0c 100644 --- a/plugins/oval/src/test/java/org/apache/struts2/oval/interceptor/SimpleFieldI18n.java +++ b/plugins/oval/src/test/java/org/apache/struts2/oval/interceptor/SimpleFieldI18n.java @@ -21,12 +21,12 @@ package org.apache.struts2.oval.interceptor; import com.opensymphony.xwork2.ActionSupport; -import net.sf.oval.constraint.NotNull; -import net.sf.oval.constraint.NotEmpty; import net.sf.oval.constraint.Length; +import net.sf.oval.constraint.NotNull; public class SimpleFieldI18n extends ActionSupport { @NotNull(message = "notnull.field") + @Length(max = 3, message = "field.too.long") private String name; public String getName() { diff --git a/plugins/oval/src/test/resources/org/apache/struts2/oval/interceptor/SimpleFieldI18n.properties b/plugins/oval/src/test/resources/org/apache/struts2/oval/interceptor/SimpleFieldI18n.properties index e2233da68..be775b767 100644 --- a/plugins/oval/src/test/resources/org/apache/struts2/oval/interceptor/SimpleFieldI18n.properties +++ b/plugins/oval/src/test/resources/org/apache/struts2/oval/interceptor/SimpleFieldI18n.properties @@ -1 +1,2 @@ -notnull.field=${field.name} cannot be null \ No newline at end of file +notnull.field=${field.name} cannot be null +field.too.long=${field.name} value is too long, allowed length is ${max} \ No newline at end of file diff --git a/plugins/oval/src/test/resources/oval-test.xml b/plugins/oval/src/test/resources/oval-test.xml index 7d07abda3..4c432a30a 100644 --- a/plugins/oval/src/test/resources/oval-test.xml +++ b/plugins/oval/src/test/resources/oval-test.xml @@ -46,6 +46,10 @@ + + + +