diff --git a/elements/src/main/java/com/manydesigns/elements/fields/ObjectField.java b/elements/src/main/java/com/manydesigns/elements/fields/ObjectField.java index 8a76b0cc2..ffc009bda 100644 --- a/elements/src/main/java/com/manydesigns/elements/fields/ObjectField.java +++ b/elements/src/main/java/com/manydesigns/elements/fields/ObjectField.java @@ -71,7 +71,11 @@ public void setValue(Object value) { @Override public void readFromRequest(HttpServletRequest req) { super.readFromRequest(req); + if (mode.isView(insertable, updatable)) { + return; + } form.readFromRequest(req); + setValueFromForm(); } @Override @@ -97,10 +101,17 @@ public void readFrom(KeyValueAccessor keyValueAccessor) { if (value == null) { setValue(null); } else { - Object object = classAccessor.newInstance(); form.readFrom(keyValueAccessor.inner(value)); - form.writeToObject(object); - this.value = object; + setValueFromForm(); + } + } + + protected void setValueFromForm() { + Object object = classAccessor.newInstance(); + if (object == null) { + throw new IllegalStateException("Could not create an instance using " + classAccessor); } + form.writeToObject(object); + this.value = object; } } diff --git a/elements/src/test/java/com/manydesigns/elements/fields/ObjectFieldTest.java b/elements/src/test/java/com/manydesigns/elements/fields/ObjectFieldTest.java index e322bd41f..ab0f27156 100644 --- a/elements/src/test/java/com/manydesigns/elements/fields/ObjectFieldTest.java +++ b/elements/src/test/java/com/manydesigns/elements/fields/ObjectFieldTest.java @@ -86,43 +86,43 @@ public void testRequiredEdit() { text); } - /* - public void testRequiredReadFromRequest() { - booleanField = new BooleanField(myPropertyAccessor, Mode.EDIT); + public void testRequiredReadFromRequest() { + var field = new ObjectField(myPropertyAccessor, Mode.EDIT); - booleanField.setRequired(true); - assertTrue(booleanField.isRequired()); + field.setRequired(true); + assertTrue(field.isRequired()); - MutableHttpServletRequest req = new MutableHttpServletRequest(); + MutableHttpServletRequest req = new MutableHttpServletRequest(); - booleanField.setValue(true); - assertTrue(booleanField.getValue()); + NestedBean n1 = new NestedBean("n1", true); + field.setValue(n1); + assertEquals(n1, field.getValue()); - req.setParameter("myBoolean", ""); - booleanField.readFromRequest(req); - assertFalse(booleanField.getValue()); + req.setParameter("nested.flag", ""); + field.readFromRequest(req); + assertFalse(((NestedBean) field.getValue()).flag); - req.setParameter("myBoolean", "true"); - booleanField.readFromRequest(req); - assertTrue(booleanField.getValue()); + req.setParameter("nested.flag", "true"); + field.readFromRequest(req); + assertTrue(((NestedBean) field.getValue()).flag); - req.setParameter("myBoolean", "false"); - booleanField.readFromRequest(req); - assertFalse(booleanField.getValue()); + req.setParameter("nested.flag", "false"); + field.readFromRequest(req); + assertFalse(((NestedBean) field.getValue()).flag); - req.setParameter("myBoolean", (String) null); - req.setParameter(BooleanField.CHECK_PREFIX + "myBoolean", "true"); - booleanField.readFromRequest(req); - assertFalse(booleanField.getValue()); - - booleanField.setValue(true); - req.setParameter("myBoolean", (String) null); - req.setParameter(BooleanField.CHECK_PREFIX + "myBoolean", (String) null); - booleanField.readFromRequest(req); - assertTrue(booleanField.getValue()); - } + req.setParameter("nested.flag", (String) null); + req.setParameter("nested." + BooleanField.CHECK_PREFIX + "flag", "true"); + field.readFromRequest(req); + assertFalse(((NestedBean) field.getValue()).flag); + field.setValue(n1); + req.setParameter("nested.flag", (String) null); + req.setParameter("nested." + BooleanField.CHECK_PREFIX + "flag", (String) null); + field.readFromRequest(req); + assertTrue(((NestedBean) field.getValue()).flag); + } + /* //-------------------------------------------------------------------------- // Not required //-------------------------------------------------------------------------- @@ -229,6 +229,8 @@ public static class NestedBean { public String name; public boolean flag; + public NestedBean() {} + public NestedBean(String name, boolean flag) { this.name = name; this.flag = flag;