Commits

Anonymous committed cc0357b

XW-156: ModelDriven actions using the visitor field validator were getting the wrong field names for field errors because it didn't know it should leave off the field prefix "model". Now there is a property on the VisitorFieldValidator "appendPrefix" which tells it whether to append the field name as the prefix (the default is true, which is the same behavior as before).

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

  • Participants
  • Parent commits 5c724bc

Comments (0)

Files changed (20)

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

         }
     }
 
-    protected static TextProvider makeTextProvider(Object object, LocaleProvider localeProvider) {
+    public static TextProvider makeTextProvider(Object object, LocaleProvider localeProvider) {
         if (object instanceof TextProvider) {
             return (TextProvider) object;
         } else {

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

     //~ Instance fields ////////////////////////////////////////////////////////
 
     private String context;
+    private boolean appendPrefix = true;
 
     //~ Methods ////////////////////////////////////////////////////////////////
 
         return context;
     }
 
+    public boolean isAppendPrefix() {
+        return appendPrefix;
+    }
+
+    public void setAppendPrefix(boolean appendPrefix) {
+        this.appendPrefix = appendPrefix;
+    }
+
     public void validate(Object object) throws ValidationException {
         String fieldName = getFieldName();
         Object value = this.getFieldValue(fieldName, object);
     private void validateObject(String fieldName, Object o, String visitorContext) throws ValidationException {
         OgnlValueStack stack = ActionContext.getContext().getValueStack();
         stack.push(o);
-
-        ValidatorContext validatorContext = new AppendingValidatorContext(getValidatorContext(), o, fieldName, getMessage(o));
+        ValidatorContext validatorContext;
+        if (appendPrefix) {
+            validatorContext = new AppendingValidatorContext(getValidatorContext(), o, fieldName, getMessage(o), appendPrefix);
+        } else {
+            ValidatorContext parent = getValidatorContext();
+            validatorContext = new DelegatingValidatorContext(parent, DelegatingValidatorContext.makeTextProvider(o, parent), parent);
+        }
         ActionValidatorManager.validate(o, visitorContext, validatorContext);
         stack.pop();
     }
         String field;
         String message;
 
-        public AppendingValidatorContext(ValidatorContext parent, Object object, String field, String message) {
+        public AppendingValidatorContext(ValidatorContext parent, Object object, String field, String message, boolean appendPrefix) {
             super(parent, makeTextProvider(object, parent), parent);
 
             //            super(parent);

src/test/com/opensymphony/xwork/VisitorValidatorTestAction-beanMessageBundle-validation.xml

-<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
-<validators>
-    <field name="bean">
-        <field-validator type="visitor">
-            <message>bean: </message>
-        </field-validator>
-    </field>
-</validators>

src/test/com/opensymphony/xwork/VisitorValidatorTestAction-validateArray-validation.xml

-<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
-<validators>
-    <field name="testBeanArray">
-        <field-validator type="visitor">
-            <message>testBeanArray: </message>
-        </field-validator>
-    </field>
-</validators>

src/test/com/opensymphony/xwork/VisitorValidatorTestAction-validateList-validation.xml

-<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
-<validators>
-    <field name="testBeanList">
-        <field-validator type="visitor">
-            <message>testBeanList: </message>
-        </field-validator>
-    </field>
-</validators>

src/test/com/opensymphony/xwork/VisitorValidatorTestAction-validation.xml

-<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
-<validators>
-    <field name="context">
-        <field-validator type="requiredstring">
-            <message>You must enter a context.</message>
-        </field-validator>
-    </field>
-</validators>

src/test/com/opensymphony/xwork/VisitorValidatorTestAction-visitorValidation-validation.xml

-<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
-<validators>
-    <field name="bean">
-        <field-validator type="visitor">
-            <message>bean: </message>
-        </field-validator>
-    </field>
-</validators>

src/test/com/opensymphony/xwork/VisitorValidatorTestAction-visitorValidationAlias-validation.xml

-<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
-<validators>
-    <field name="bean">
-        <field-validator type="visitor">
-            <param name="context">anotherContext</param>
-            <message>bean: </message>
-        </field-validator>
-    </field>
-</validators>

src/test/com/opensymphony/xwork/VisitorValidatorTestAction.java

-/*
- * Copyright (c) 2002-2003 by OpenSymphony
- * All rights reserved.
- */
-package com.opensymphony.xwork;
-
-import java.util.ArrayList;
-import java.util.List;
-
-
-/**
- * VisitorValidatorTestAction
- * @author Jason Carreira
- * Created Aug 4, 2003 1:00:04 AM
- */
-public class VisitorValidatorTestAction extends ActionSupport {
-    //~ Instance fields ////////////////////////////////////////////////////////
-
-    private List testBeanList = new ArrayList();
-    private String context;
-    private TestBean bean = new TestBean();
-    private TestBean[] testBeanArray;
-
-    //~ Constructors ///////////////////////////////////////////////////////////
-
-    public VisitorValidatorTestAction() {
-        testBeanArray = new TestBean[5];
-
-        for (int i = 0; i < 5; i++) {
-            testBeanArray[i] = new TestBean();
-            testBeanList.add(new TestBean());
-        }
-    }
-
-    //~ Methods ////////////////////////////////////////////////////////////////
-
-    public void setBean(TestBean bean) {
-        this.bean = bean;
-    }
-
-    public TestBean getBean() {
-        return bean;
-    }
-
-    public void setContext(String context) {
-        this.context = context;
-    }
-
-    public String getContext() {
-        return context;
-    }
-
-    public void setTestBeanArray(TestBean[] testBeanArray) {
-        this.testBeanArray = testBeanArray;
-    }
-
-    public TestBean[] getTestBeanArray() {
-        return testBeanArray;
-    }
-
-    public void setTestBeanList(List testBeanList) {
-        this.testBeanList = testBeanList;
-    }
-
-    public List getTestBeanList() {
-        return testBeanList;
-    }
-}

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

+package com.opensymphony.xwork.validator;
+
+import junit.framework.TestCase;
+
+import java.util.*;
+
+import com.opensymphony.xwork.TestBean;
+import com.opensymphony.xwork.ActionContext;
+import com.opensymphony.xwork.util.OgnlValueStack;
+
+/**
+ * VisitorFieldValidatorModelTest
+ * @author Jason Carreira
+ * Date: Mar 18, 2004 2:51:42 PM
+ */
+public class VisitorFieldValidatorModelTest extends TestCase {
+
+    protected VisitorValidatorModelAction action;
+    private Locale origLocale;
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public void setUp() {
+        origLocale = Locale.getDefault();
+        Locale.setDefault(Locale.US);
+
+        action = new VisitorValidatorModelAction();
+
+        TestBean bean = action.getBean();
+        Calendar cal = new GregorianCalendar(1900, 01, 01);
+        bean.setBirth(cal.getTime());
+        bean.setCount(-1);
+
+        OgnlValueStack stack = new OgnlValueStack();
+        ActionContext.setContext(new ActionContext(stack.getContext()));
+    }
+
+    public void testModelFieldErrorsAddedWithoutFieldPrefix() throws Exception {
+        ActionValidatorManager.validate(action,null);
+        assertTrue(action.hasFieldErrors());
+
+        Map fieldErrors = action.getFieldErrors();
+        // the required string validation inherited from the VisitorValidatorTestAction
+        assertTrue(fieldErrors.containsKey("context"));
+        // the bean validation which is now at the top level because we set the appendPrefix to false
+        assertTrue(fieldErrors.containsKey("name"));
+
+        List nameMessages = (List) fieldErrors.get("name");
+        assertEquals(1, nameMessages.size());
+
+        String nameMessage = (String) nameMessages.get(0);
+        assertEquals("You must enter a name.", nameMessage);
+    }
+
+    protected void tearDown() throws Exception {
+        super.tearDown();
+        ActionContext.setContext(null);
+        Locale.setDefault(origLocale);
+    }
+
+
+}

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

 
 import com.opensymphony.xwork.ActionContext;
 import com.opensymphony.xwork.TestBean;
-import com.opensymphony.xwork.VisitorValidatorTestAction;
 import com.opensymphony.xwork.util.OgnlValueStack;
 
 import junit.framework.TestCase;

src/test/com/opensymphony/xwork/validator/VisitorValidatorModelAction-validation.xml

+<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
+<validators>
+    <field name="model">
+        <field-validator type="visitor">
+            <param name="appendPrefix">false</param>
+            <message>model: </message>
+        </field-validator>
+    </field>
+</validators>

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

+package com.opensymphony.xwork.validator;
+
+import com.opensymphony.xwork.ModelDriven;
+
+/**
+ * VisitorValidatorModelAction
+ * @author Jason Carreira
+ * Date: Mar 18, 2004 11:26:46 AM
+ */
+public class VisitorValidatorModelAction extends VisitorValidatorTestAction implements ModelDriven {
+    /**
+     * @return the model to be pushed onto the ValueStack instead of the Action itself
+     */
+    public Object getModel() {
+        return getBean();
+    }
+}

src/test/com/opensymphony/xwork/validator/VisitorValidatorTestAction-beanMessageBundle-validation.xml

+<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
+<validators>
+    <field name="bean">
+        <field-validator type="visitor">
+            <message>bean: </message>
+        </field-validator>
+    </field>
+</validators>

src/test/com/opensymphony/xwork/validator/VisitorValidatorTestAction-validateArray-validation.xml

+<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
+<validators>
+    <field name="testBeanArray">
+        <field-validator type="visitor">
+            <message>testBeanArray: </message>
+        </field-validator>
+    </field>
+</validators>

src/test/com/opensymphony/xwork/validator/VisitorValidatorTestAction-validateList-validation.xml

+<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
+<validators>
+    <field name="testBeanList">
+        <field-validator type="visitor">
+            <message>testBeanList: </message>
+        </field-validator>
+    </field>
+</validators>

src/test/com/opensymphony/xwork/validator/VisitorValidatorTestAction-validation.xml

+<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
+<validators>
+    <field name="context">
+        <field-validator type="requiredstring">
+            <message>You must enter a context.</message>
+        </field-validator>
+    </field>
+</validators>

src/test/com/opensymphony/xwork/validator/VisitorValidatorTestAction-visitorValidation-validation.xml

+<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
+<validators>
+    <field name="bean">
+        <field-validator type="visitor">
+            <message>bean: </message>
+        </field-validator>
+    </field>
+</validators>

src/test/com/opensymphony/xwork/validator/VisitorValidatorTestAction-visitorValidationAlias-validation.xml

+<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
+<validators>
+    <field name="bean">
+        <field-validator type="visitor">
+            <param name="context">anotherContext</param>
+            <message>bean: </message>
+        </field-validator>
+    </field>
+</validators>

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

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork.validator;
+
+import com.opensymphony.xwork.ActionSupport;
+import com.opensymphony.xwork.TestBean;
+
+import java.util.ArrayList;
+import java.util.List;
+
+
+/**
+ * VisitorValidatorTestAction
+ * @author Jason Carreira
+ * Created Aug 4, 2003 1:00:04 AM
+ */
+public class VisitorValidatorTestAction extends ActionSupport {
+    //~ Instance fields ////////////////////////////////////////////////////////
+
+    private List testBeanList = new ArrayList();
+    private String context;
+    private TestBean bean = new TestBean();
+    private TestBean[] testBeanArray;
+
+    //~ Constructors ///////////////////////////////////////////////////////////
+
+    public VisitorValidatorTestAction() {
+        testBeanArray = new TestBean[5];
+
+        for (int i = 0; i < 5; i++) {
+            testBeanArray[i] = new TestBean();
+            testBeanList.add(new TestBean());
+        }
+    }
+
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public void setBean(TestBean bean) {
+        this.bean = bean;
+    }
+
+    public TestBean getBean() {
+        return bean;
+    }
+
+    public void setContext(String context) {
+        this.context = context;
+    }
+
+    public String getContext() {
+        return context;
+    }
+
+    public void setTestBeanArray(TestBean[] testBeanArray) {
+        this.testBeanArray = testBeanArray;
+    }
+
+    public TestBean[] getTestBeanArray() {
+        return testBeanArray;
+    }
+
+    public void setTestBeanList(List testBeanList) {
+        this.testBeanList = testBeanList;
+    }
+
+    public List getTestBeanList() {
+        return testBeanList;
+    }
+}