WW-3432 pushes messages variables to allow customize validation messages

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1311945 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Lukasz Lenart
2012-04-10 19:29:09 +00:00
parent b90fe0996f
commit ecc0e56440
5 changed files with 41 additions and 8 deletions
@@ -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)) {
@@ -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<String, List<String>> 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<String, List<String>> 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();
@@ -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() {
@@ -1 +1,2 @@
notnull.field=${field.name} cannot be null
notnull.field=${field.name} cannot be null
field.too.long=${field.name} value is too long, allowed length is ${max}
@@ -46,6 +46,10 @@
<interceptor-ref name="ovalValidation"/>
<result type="void"></result>
</action>
<action name="simpleFieldTooLong" class="org.apache.struts2.oval.interceptor.SimpleField">
<interceptor-ref name="ovalValidation"/>
<result type="void"></result>
</action>
<action name="simpleMethod" class="org.apache.struts2.oval.interceptor.SimpleMethod">
<interceptor-ref name="ovalValidation"/>
<result type="void"></result>