Author: jdonnerstag
Date: Sat Apr 4 07:59:59 2009
New Revision: 761875
URL: http://svn.apache.org/viewvc?rev=761875&view=rev
Log:
fixed WICKET-2202 (and also WICKET-2026): Form gets submitted using
AjaxSubmitBehavior when sub-form has error's
Issue: WICKET-2202
Added:
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/HomePageTest.java
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/MyPanel.html
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/MyPanel.java
Removed:
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/TestHomePage.java
Modified:
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/HomePage.html
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/HomePage.java
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/MyBorder.java
Modified:
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java?rev=761875&r1=761874&r2=761875&view=diff
==============================================================================
---
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java
(original)
+++
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java
Sat Apr 4 07:59:59 2009
@@ -1200,12 +1200,11 @@
{
final boolean[] error = new boolean[] { false };
- final FormComponent.IVisitor visitor = new
FormComponent.IVisitor()
+ final IVisitor<Component> visitor = new IVisitor<Component>()
{
- public Object formComponent(IFormVisitorParticipant
formComponent)
+ public Object component(final Component component)
{
- if (formComponent instanceof Component &&
-
((Component)formComponent).hasErrorMessage())
+ if (component.hasErrorMessage())
{
error[0] = true;
return
Component.IVisitor.STOP_TRAVERSAL;
@@ -1220,13 +1219,29 @@
{
public Object component(final Component component)
{
- return
visitor.formComponent((FormComponent<?>)component);
+ return visitor.component(component);
}
});
if (!error[0])
{
- visitChildrenInContainingBorder(visitor);
+ if (getParent() instanceof Border)
+ {
+ MarkupContainer border = getParent();
+ Iterator<? extends Component> iter =
border.iterator();
+ while (iter.hasNext())
+ {
+ Component child = iter.next();
+ if ((child != this) && (child
instanceof FormComponent))
+ {
+ visitor.component(child);
+ if (error[0])
+ {
+ break;
+ }
+ }
+ }
+ }
}
return error[0];
@@ -1620,7 +1635,7 @@
// override default message
final String defaultValue = "Upload
must be less than " + getMaxSize();
String msg = getString(getId() + "." +
UPLOAD_TOO_LARGE_RESOURCE_KEY,
- Model.of(model), defaultValue);
+ Model.ofMap(model),
defaultValue);
error(msg);
}
else
@@ -1629,7 +1644,7 @@
// default message
final String defaultValue = "Upload
failed: " + e.getLocalizedMessage();
String msg = getString(getId() + "." +
UPLOAD_FAILED_RESOURCE_KEY,
- Model.of(model), defaultValue);
+ Model.ofMap(model),
defaultValue);
error(msg);
log.warn(msg, e);
Modified:
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/HomePage.html
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/HomePage.html?rev=761875&r1=761874&r2=761875&view=diff
==============================================================================
---
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/HomePage.html
(original)
+++
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/HomePage.html
Sat Apr 4 07:59:59 2009
@@ -18,5 +18,11 @@
<input wicket:id="textfield1" /> - <span
wicket:id="lbltextfield1" /><br />
<input wicket:id="textfield2" /> - <span
wicket:id="lbltextfield2" /><br />
</div>
+
+ <form wicket:id="form3">
+ <div wicket:id="panel">[panel]</div>
+ <button wicket:id="submit" type="submit">submit</button>
+ <button wicket:id="submit2"
type="submit">submit</button>
+ </form>
</body>
</html>
Modified:
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/HomePage.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/HomePage.java?rev=761875&r1=761874&r2=761875&view=diff
==============================================================================
---
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/HomePage.java
(original)
+++
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/HomePage.java
Sat Apr 4 07:59:59 2009
@@ -22,6 +22,7 @@
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
+import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;
@@ -39,6 +40,15 @@
/** */
public String textfield1, textfield2;
+ /** */
+ public int formSubmitted = 0;
+
+ public static int AJAX = 2;
+ public static int NORMAL = 4;
+
+ boolean hitOnSubmit = false;
+ boolean hitOnError = false;
+
/**
* Constructor that is invoked when page is invoked without a session.
*
@@ -47,7 +57,6 @@
*/
public HomePage(final PageParameters parameters)
{
-
// Add the simplest type of label
add(new Label("message",
"If you see this message wicket is properly configured
and running"));
@@ -74,6 +83,7 @@
protected void onSubmit(AjaxRequestTarget target,
Form<?> form)
{
info("onSubmit");
+ hitOnSubmit = true;
target.addComponent(form);
}
@@ -81,6 +91,7 @@
protected void onError(AjaxRequestTarget target,
Form<?> form)
{
error("onError");
+ hitOnError = true;
target.addComponent(form);
}
});
@@ -97,5 +108,50 @@
border.add(new TextField<String>("textfield2",
new PropertyModel<String>(this, "textfield2")));
border.add(new Label("lbltextfield2", new
PropertyModel<String>(this, "textfield2")));
+
+ // --------------------
+
+ Form<Void> form3 = new Form<Void>("form3");
+ MyPanel panel = new MyPanel("panel");
+ form3.add(panel);
+ form3.add(new AjaxSubmitLink("submit")
+ {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ protected void onSubmit(AjaxRequestTarget target, Form
form)
+ {
+ formSubmitted = formSubmitted | AJAX;
+ target.addComponent(form);
+ }
+ });
+ form3.add(new SubmitLink("submit2")
+ {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ public void onSubmit()
+ {
+ formSubmitted = formSubmitted | NORMAL;
+ }
+ });
+ add(form3);
+ }
+
+ public int getFormSubmitted()
+ {
+ return formSubmitted;
+ }
+
+ /**
+ * @see org.apache.wicket.Page#onBeforeRender()
+ */
+ @Override
+ protected void onBeforeRender()
+ {
+ hitOnSubmit = false;
+ hitOnError = false;
+
+ super.onBeforeRender();
}
}
Added:
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/HomePageTest.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/HomePageTest.java?rev=761875&view=auto
==============================================================================
---
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/HomePageTest.java
(added)
+++
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/HomePageTest.java
Sat Apr 4 07:59:59 2009
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.markup.html.form.validation;
+
+import junit.framework.TestCase;
+
+import org.apache.wicket.feedback.FeedbackMessage;
+import org.apache.wicket.markup.html.form.TextField;
+import org.apache.wicket.util.tester.FormTester;
+import org.apache.wicket.util.tester.WicketTester;
+
+/**
+ * Simple test using the WicketTester
+ */
+public class HomePageTest extends TestCase
+{
+ private WicketTester tester;
+
+ @Override
+ public void setUp()
+ {
+ tester = new WicketTester();
+ tester.startPage(HomePage.class);
+ tester.assertRenderedPage(HomePage.class);
+ }
+
+ /**
+ *
+ */
+ public void testWithoutBorder()
+ {
+ tester.executeAjaxEvent("form:submit", "onclick");
+ assertEquals("Expected one error message",
+ tester.getMessages(FeedbackMessage.ERROR).size(), 2);
+ assertTrue(((HomePage)tester.getLastRenderedPage()).hitOnError);
+
assertFalse(((HomePage)tester.getLastRenderedPage()).hitOnSubmit);
+ }
+
+ /**
+ *
+ */
+ public void testWithoutBorder2()
+ {
+ FormTester formTester = tester.newFormTester("form");
+ formTester.setValue("textfield1", "testxxx");
+ tester.executeAjaxEvent("form:submit", "onclick");
+ tester.assertNoErrorMessage();
+
assertFalse(((HomePage)tester.getLastRenderedPage()).hitOnError);
+
assertTrue(((HomePage)tester.getLastRenderedPage()).hitOnSubmit);
+ }
+
+ /**
+ *
+ */
+ public void testWithBorder()
+ {
+ tester.executeAjaxEvent("border:form2:submit", "onclick");
+ assertEquals("Expected one error message",
+ tester.getMessages(FeedbackMessage.ERROR).size(), 2);
+
assertTrue(((MyBorder)tester.getLastRenderedPage().get("border")).hitOnError);
+
assertFalse(((MyBorder)tester.getLastRenderedPage().get("border")).hitOnSubmit);
+ }
+
+ /**
+ *
+ */
+ public void testWithBorder2()
+ {
+ TextField textfield =
(TextField)tester.getLastRenderedPage().get("border:textfield1");
+
tester.getServletRequest().setParameter(textfield.getInputName(), "abcde");
+ tester.executeAjaxEvent("border:form2:submit", "onclick");
+ tester.assertNoErrorMessage();
+
assertFalse(((MyBorder)tester.getLastRenderedPage().get("border")).hitOnError);
+
assertTrue(((MyBorder)tester.getLastRenderedPage().get("border")).hitOnSubmit);
+ }
+
+ public void testWithPanelAjax()
+ {
+ tester.executeAjaxEvent("form3:submit", "onclick");
+
+ HomePage page = (HomePage)tester.getLastRenderedPage();
+ assertTrue((page.getFormSubmitted() & HomePage.AJAX) ==
HomePage.AJAX);
+ }
+
+ /**
+ *
+ */
+ public void testWithPanelForm()
+ {
+ tester.clickLink("form3:submit2");
+ HomePage page = (HomePage)tester.getLastRenderedPage();
+ assertTrue((page.getFormSubmitted() & HomePage.NORMAL) ==
HomePage.NORMAL);
+ }
+}
Modified:
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/MyBorder.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/MyBorder.java?rev=761875&r1=761874&r2=761875&view=diff
==============================================================================
---
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/MyBorder.java
(original)
+++
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/MyBorder.java
Sat Apr 4 07:59:59 2009
@@ -30,6 +30,9 @@
private static final long serialVersionUID = 1L;
Form<Void> form;
+ boolean hitOnSubmit = false;
+ boolean hitOnError = false;
+
/**
* Construct.
*
@@ -53,6 +56,7 @@
protected void onSubmit(AjaxRequestTarget target,
Form<?> form)
{
info("onSubmit");
+ hitOnSubmit = true;
target.addComponent(form);
}
@@ -60,6 +64,7 @@
protected void onError(AjaxRequestTarget target,
Form<?> form)
{
error("onError");
+ hitOnError = true;
target.addComponent(form);
}
});
@@ -68,6 +73,9 @@
@Override
protected void onBeforeRender()
{
+ hitOnSubmit = false;
+ hitOnError = false;
+
super.onBeforeRender();
form.add(getBodyContainer());
}
Added:
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/MyPanel.html
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/MyPanel.html?rev=761875&view=auto
==============================================================================
---
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/MyPanel.html
(added)
+++
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/MyPanel.html
Sat Apr 4 07:59:59 2009
@@ -0,0 +1,18 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html
+ xmlns="http://www.w3.org/1999/xhtml"
+ xmlns:wicket="http://wicket.apache.org/"
+>
+<head>
+</head>
+<body>
+<wicket:panel>
+
+<form wicket:id="form3">
+ <div wicket:id="feedback" />
+ <input wicket:id="textfield1" /> - <span wicket:id="lbltextfield1"
/><br />
+</form>
+
+</wicket:panel>
+</body>
+</html>
Added:
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/MyPanel.java
URL:
http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/MyPanel.java?rev=761875&view=auto
==============================================================================
---
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/MyPanel.java
(added)
+++
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/validation/MyPanel.java
Sat Apr 4 07:59:59 2009
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.markup.html.form.validation;
+
+import org.apache.wicket.markup.html.basic.Label;
+import org.apache.wicket.markup.html.form.Form;
+import org.apache.wicket.markup.html.form.FormComponent;
+import org.apache.wicket.markup.html.form.TextField;
+import org.apache.wicket.markup.html.panel.FeedbackPanel;
+import org.apache.wicket.markup.html.panel.Panel;
+import org.apache.wicket.model.PropertyModel;
+
+public class MyPanel extends Panel
+{
+
+ private static final long serialVersionUID = 1L;
+
+ public String textfield1;
+
+
+ /**
+ * Construct.
+ *
+ * @param id
+ */
+ public MyPanel(String id)
+ {
+ super(id);
+
+ Form<Void> form = new Form<Void>("form3");
+ form.setOutputMarkupId(true);
+ add(form);
+
+ form.add(new FeedbackPanel("feedback"));
+ form.add(new TextField<String>("textfield1", new
PropertyModel<String>(this, "textfield1")));
+ form.add(new Label("lbltextfield1", new
PropertyModel<String>(this, "textfield1")));
+
+ form.add(new AbstractFormValidator()
+ {
+
+ public FormComponent<?>[] getDependentFormComponents()
+ {
+ return null;
+ }
+
+ public void validate(Form<?> form)
+ {
+ form.error("form validation error");
+ }
+
+ });
+
+ }
+
+}