Anonymous avatar Anonymous committed f89f8a2

XW-86: VisitorFieldValidator now adds object property name to field names for errors, so that error messages will be added to field errors as object.property

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@37 e221344d-f017-0410-9bd5-d282ab1896d7

Comments (0)

Files changed (3)

src/java/com/opensymphony/xwork/validator/DelegatingValidatorContext.java

 
 import java.util.Collection;
 import java.util.Iterator;
+import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.ResourceBundle;
         return localeAware.getText(aTextName, defaultValue);
     }
 
+    public String getText(String aTextName, List args) {
+        return localeAware.getText(aTextName, args);
+    }
+
     public ResourceBundle getTexts(String aBundleName) {
         return localeAware.getTexts(aBundleName);
     }

src/java/com/opensymphony/xwork/validator/validators/VisitorFieldValidator.java

 
             for (Iterator iterator = coll.iterator(); iterator.hasNext();) {
                 Object o = iterator.next();
-                ValidatorContext validatorContext = new AppendingValidatorContext(getValidatorContext(), getMessage(o));
+                ValidatorContext validatorContext = new AppendingValidatorContext(getValidatorContext(), fieldName, getMessage(o));
                 ActionValidatorManager.validate(o, visitorContext, validatorContext);
             }
         } else if (value instanceof Object[]) {
 
             for (int i = 0; i < array.length; i++) {
                 Object o = array[i];
-                ValidatorContext validatorContext = new AppendingValidatorContext(getValidatorContext(), getMessage(o));
+                ValidatorContext validatorContext = new AppendingValidatorContext(getValidatorContext(), fieldName, getMessage(o));
                 ActionValidatorManager.validate(o, visitorContext, validatorContext);
             }
         } else {
-            ValidatorContext validatorContext = new AppendingValidatorContext(getValidatorContext(), getMessage(value));
+            ValidatorContext validatorContext = new AppendingValidatorContext(getValidatorContext(), fieldName, getMessage(value));
             ActionValidatorManager.validate(value, visitorContext, validatorContext);
         }
     }
     //~ Inner Classes //////////////////////////////////////////////////////////
 
     private class AppendingValidatorContext extends DelegatingValidatorContext {
+        String field;
         String message;
 
-        public AppendingValidatorContext(Object object, String message) {
+        public AppendingValidatorContext(Object object, String field, String message) {
             super(object);
+            this.field = field;
             this.message = message;
         }
 
         public void addActionError(String anErrorMessage) {
-            super.addActionError(message + anErrorMessage);
+            super.addFieldError(field, message + anErrorMessage);
         }
 
         public void addFieldError(String fieldName, String errorMessage) {
-            super.addFieldError(fieldName, message + errorMessage);
+            super.addFieldError(field + "." + fieldName, message + errorMessage);
         }
     }
 }

src/test/com/opensymphony/xwork/validator/VisitorFieldValidatorTest.java

 
         Map fieldErrors = action.getFieldErrors();
         assertEquals(2, fieldErrors.size());
-        assertTrue(fieldErrors.containsKey("name"));
+        assertTrue(fieldErrors.containsKey("testBeanArray.name"));
 
         //the error from the action should be there too
         assertTrue(fieldErrors.containsKey("context"));
 
-        List errors = (List) fieldErrors.get("name");
+        List errors = (List) fieldErrors.get("testBeanArray.name");
         assertEquals(4, errors.size());
     }
 
 
         Map fieldErrors = action.getFieldErrors();
         assertEquals(2, fieldErrors.size());
-        assertTrue(fieldErrors.containsKey("name"));
+        assertTrue(fieldErrors.containsKey("testBeanList.name"));
 
         //the error from the action should be there too
         assertTrue(fieldErrors.containsKey("context"));
 
-        List errors = (List) fieldErrors.get("name");
+        List errors = (List) fieldErrors.get("testBeanList.name");
         assertEquals(4, errors.size());
     }
 
 
         Map fieldErrors = action.getFieldErrors();
         assertEquals(3, fieldErrors.size());
-        assertTrue(fieldErrors.containsKey("count"));
-        assertTrue(fieldErrors.containsKey("name"));
-        assertTrue(!fieldErrors.containsKey("birth"));
+        assertTrue(fieldErrors.containsKey("bean.count"));
+        assertTrue(fieldErrors.containsKey("bean.name"));
+        assertTrue(!fieldErrors.containsKey("bean.birth"));
 
         //the error from the action should be there too
         assertTrue(fieldErrors.containsKey("context"));
 
         Map fieldErrors = action.getFieldErrors();
         assertEquals(3, fieldErrors.size());
-        assertTrue(!fieldErrors.containsKey("count"));
-        assertTrue(fieldErrors.containsKey("name"));
-        assertTrue(fieldErrors.containsKey("birth"));
+        assertTrue(!fieldErrors.containsKey("bean.count"));
+        assertTrue(fieldErrors.containsKey("bean.name"));
+        assertTrue(fieldErrors.containsKey("bean.birth"));
 
         //the error from the action should be there too
         assertTrue(fieldErrors.containsKey("context"));
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.