You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by pa...@apache.org on 2020/02/06 12:13:34 UTC

[wicket] branch master updated: WICKET-6737: submit form via triggered submit event

This is an automated email from the ASF dual-hosted git repository.

papegaaij pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/wicket.git


The following commit(s) were added to refs/heads/master by this push:
     new 6b8ca7d  WICKET-6737: submit form via triggered submit event
6b8ca7d is described below

commit 6b8ca7dde11c68344b2e2259e3ba7c55b375a8f5
Author: Emond Papegaaij <em...@topicus.nl>
AuthorDate: Wed Feb 5 22:16:20 2020 +0100

    WICKET-6737: submit form via triggered submit event
    
    This will also invoke any inline onsubmit handler on the form. Via
    shouldTriggerJavaScriptSubmitEvent on SubmitLink (was
    shouldInvokeJavaScriptFormOnsubmit) controls whether the event is used
    or f.submit() is executed. The latter will not trigger any event
    handlers.
---
 .../java/org/apache/wicket/markup/html/form/Form.java | 19 +++++++++++++++----
 .../apache/wicket/markup/html/form/SubmitLink.java    | 16 +++++-----------
 .../org/apache/wicket/markup/html/form/FormTest.java  |  4 ++--
 .../wicket/examples/compref/SubmitLinkPage.html       |  2 +-
 .../wicket/examples/compref/SubmitLinkPage.java       | 15 +++++++++++++--
 5 files changed, 36 insertions(+), 20 deletions(-)

diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java
index 3dd89e6..050264f 100644
--- a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/Form.java
@@ -553,20 +553,24 @@ public class Form<T> extends WebMarkupContainer
 		}
 		buffer.append(String.format("var f = document.getElementById('%s');", root.getMarkupId()));
 		buffer.append(String.format("f.action='%s';", action));
-		buffer.append("f.submit();");
+		buffer.append("Wicket.Event.fire(f, 'submit');");
 		return buffer;
 	}
 
 	/**
-	 * Generate a piece of JavaScript that submits the form with the given {@link IFormSubmittingComponent}.
+	 * Generate a piece of JavaScript that submits the form with the given
+	 * {@link IFormSubmittingComponent}.
 	 * 
 	 * @param submitter
 	 *            the submitter
+	 * @param triggerEvent
+	 *            When true, the form will be submited via a javascript submit event, when false via
+	 *            the {@code submit()} method.
 	 * @return the javascript code that submits the form.
 	 * 
 	 * @see #findSubmitter()
 	 */
-	public final CharSequence getJsForSubmitter(IFormSubmittingComponent submitter)
+	public final CharSequence getJsForSubmitter(IFormSubmittingComponent submitter, boolean triggerEvent)
 	{
 		Form<?> root = getRootForm();
 
@@ -588,7 +592,14 @@ public class Form<T> extends WebMarkupContainer
 			String action = root.getActionUrl().toString();
 			buffer.append("f.action += '" + (action.indexOf('?') > -1 ? '&' : '?') + param + "';");
 		}
-		buffer.append("f.submit();");
+		if (triggerEvent)
+		{
+			buffer.append("Wicket.Event.fire(f, 'submit');");
+		}
+		else
+		{
+			buffer.append("f.submit();");
+		}
 		return buffer;
 	}
 
diff --git a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/SubmitLink.java b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/SubmitLink.java
index 558f8d4..c67b8e9 100644
--- a/wicket-core/src/main/java/org/apache/wicket/markup/html/form/SubmitLink.java
+++ b/wicket-core/src/main/java/org/apache/wicket/markup/html/form/SubmitLink.java
@@ -193,12 +193,12 @@ public class SubmitLink extends AbstractSubmitLink
 	}
 
 	/**
-	 * Controls whether or not clicking on this link will invoke form's javascript onsubmit handler.
-	 * True by default.
+	 * Controls whether or not clicking on this link will trigger a javascript submit event, firing
+	 * any submit handler added to the form. True by default.
 	 * 
-	 * @return true if form's javascript onsubmit handler should be invoked, false otherwise
+	 * @return true if form's javascript submit handlers should be invoked, false otherwise
 	 */
-	protected boolean shouldInvokeJavaScriptFormOnsubmit()
+	protected boolean shouldTriggerJavaScriptSubmitEvent()
 	{
 		return true;
 	}
@@ -217,13 +217,7 @@ public class SubmitLink extends AbstractSubmitLink
 			Form<?> root = getForm().getRootForm();
 
 			StringBuilder script = new StringBuilder();
-			if (shouldInvokeJavaScriptFormOnsubmit())
-			{
-				script.append(String.format("var ff=document.getElementById('%s');", getForm().getMarkupId()));
-				script.append("if (typeof ff.onsubmit === 'function' && ff.onsubmit() == false) return false;");
-			}
-			
-			script.append(root.getJsForSubmitter(this));
+			script.append(root.getJsForSubmitter(this, shouldTriggerJavaScriptSubmitEvent()));
 			script.append("return false;");
 			
 			return script;
diff --git a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormTest.java b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormTest.java
index f6275f9..9994cda 100644
--- a/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormTest.java
+++ b/wicket-core/src/test/java/org/apache/wicket/markup/html/form/FormTest.java
@@ -76,11 +76,11 @@ public class FormTest extends WicketTestCase
 		FormMethodTestPage page = (FormMethodTestPage)tester.getLastRenderedPage();
 	
 		// form action contains path and query 
-		assertEquals("var f = document.getElementById('formpost1');f.action='path?param1&param2=val%20ue';f.submit();",
+		assertEquals("var f = document.getElementById('formpost1');f.action='path?param1&param2=val%20ue';Wicket.Event.fire(f, 'submit');",
 			page.postForm.getJsForListenerUrl("path?param1&param2=val%20ue").toString());
 		
 		// form action must not contain query (since ignored by browser), put into hidden form parameters instead 
-		assertEquals("document.getElementById('formget2_hf_0').innerHTML = '<input type=\"hidden\" name=\"param1\" value=\"\" /><input type=\"hidden\" name=\"param2\" value=\"val ue\" />';var f = document.getElementById('formget2');f.action='path';f.submit();",
+		assertEquals("document.getElementById('formget2_hf_0').innerHTML = '<input type=\"hidden\" name=\"param1\" value=\"\" /><input type=\"hidden\" name=\"param2\" value=\"val ue\" />';var f = document.getElementById('formget2');f.action='path';Wicket.Event.fire(f, 'submit');",
 			page.getForm.getJsForListenerUrl("path?param1&param2=val%20ue").toString());
 	}
 
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/compref/SubmitLinkPage.html b/wicket-examples/src/main/java/org/apache/wicket/examples/compref/SubmitLinkPage.html
index e5bfee3..fdbc87a 100644
--- a/wicket-examples/src/main/java/org/apache/wicket/examples/compref/SubmitLinkPage.html
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/compref/SubmitLinkPage.html
@@ -14,7 +14,7 @@
 	It can be inside the form component or outside the form component.
 	</p>
 	<p>
-	 <form wicket:id="form" onsubmit="return confirm('Do you really want to submit?');">
+	 <form wicket:id="form">
 	  <a wicket:id="internal">Internal SubmitLink</a>
 	 </form>
 	  <a wicket:id="external">External SubmitLink with confirmation</a>
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/compref/SubmitLinkPage.java b/wicket-examples/src/main/java/org/apache/wicket/examples/compref/SubmitLinkPage.java
index 0a96d83..b98b88b 100644
--- a/wicket-examples/src/main/java/org/apache/wicket/examples/compref/SubmitLinkPage.java
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/compref/SubmitLinkPage.java
@@ -17,6 +17,8 @@
 package org.apache.wicket.examples.compref;
 
 import org.apache.wicket.examples.WicketExamplePage;
+import org.apache.wicket.markup.head.IHeaderResponse;
+import org.apache.wicket.markup.head.OnEventHeaderItem;
 import org.apache.wicket.markup.html.form.Form;
 import org.apache.wicket.markup.html.form.SubmitLink;
 import org.apache.wicket.markup.html.panel.FeedbackPanel;
@@ -39,13 +41,22 @@ public class SubmitLinkPage extends WicketExamplePage
 		add(feedbackPanel);
 
 		// Add a form with 2 SubmitLinks that can be called
-		Form<?> form = new Form("form");
+		Form< ? > form = new Form("form")
+		{
+			@Override
+			public void renderHead(IHeaderResponse response)
+			{
+				super.renderHead(response);
+				response.render(OnEventHeaderItem.forComponent(this, "submit",
+					"return confirm('Do you really want to submit?')"));
+			}
+		};
 		add(form);
 
 		SubmitLink internal = new SubmitLink("internal")
 		{
 			@Override
-			protected boolean shouldInvokeJavaScriptFormOnsubmit()
+			protected boolean shouldTriggerJavaScriptSubmitEvent()
 			{
 				return false;
 			}