You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by iv...@apache.org on 2007/01/04 22:45:32 UTC

svn commit: r492763 - /incubator/wicket/trunk/wicket/src/main/java/wicket/ajax/form/AjaxFormSubmitBehavior.java

Author: ivaynberg
Date: Thu Jan  4 13:45:31 2007
New Revision: 492763

URL: http://svn.apache.org/viewvc?view=rev&rev=492763
Log:
we dont need to pass in a form, we can look it up in the hieararchy if the component that has this behavior is inside a form

Modified:
    incubator/wicket/trunk/wicket/src/main/java/wicket/ajax/form/AjaxFormSubmitBehavior.java

Modified: incubator/wicket/trunk/wicket/src/main/java/wicket/ajax/form/AjaxFormSubmitBehavior.java
URL: http://svn.apache.org/viewvc/incubator/wicket/trunk/wicket/src/main/java/wicket/ajax/form/AjaxFormSubmitBehavior.java?view=diff&rev=492763&r1=492762&r2=492763
==============================================================================
--- incubator/wicket/trunk/wicket/src/main/java/wicket/ajax/form/AjaxFormSubmitBehavior.java (original)
+++ incubator/wicket/trunk/wicket/src/main/java/wicket/ajax/form/AjaxFormSubmitBehavior.java Thu Jan  4 13:45:31 2007
@@ -16,6 +16,7 @@
  */
 package wicket.ajax.form;
 
+import wicket.Component;
 import wicket.ajax.AjaxEventBehavior;
 import wicket.ajax.AjaxRequestTarget;
 import wicket.ajax.ClientEvent;
@@ -46,6 +47,17 @@
 
 	private Form form;
 
+	/**
+	 * Constructor. This constructor can only be used when the component this
+	 * behavior is attached to is inside a form.
+	 * 
+	 * @param event
+	 *            javascript event this behavior is attached to, like onclick
+	 */
+	public AjaxFormSubmitBehavior(ClientEvent event)
+	{
+		this(null, event);
+	}
 
 	/**
 	 * Construct.
@@ -61,12 +73,37 @@
 		this.form = form;
 	}
 
+	private Form getForm()
+	{
+		if (form == null)
+		{
+			// try to find form in the hierarchy of owning component
+			Component cursor = getComponent();
+			while (cursor != null && !(cursor instanceof Form))
+			{
+				cursor = cursor.getParent();
+			}
+			if (cursor == null)
+			{
+				throw new IllegalStateException(
+						"form was not specified in the constructor and cannot "
+								+ "be found in the hierarchy of the component this behavior "
+								+ "is attached to");
+			}
+			else
+			{
+				form = (Form)cursor;
+			}
+		}
+		return form;
+	}
+
 	@Override
 	protected CharSequence getEventHandler()
 	{
-		// get the form we are really going to submit 
-		final Form form = this.form.getRootForm();
-		
+		// get the form we are really going to submit
+		final Form form = getForm().getRootForm();
+
 		final String formId = form.getMarkupId();
 		final CharSequence url = getCallbackUrl();
 
@@ -76,7 +113,8 @@
 
 		if (getComponent() instanceof IFormSubmittingComponent)
 		{
-			call.append("'").append(((IFormSubmittingComponent)getComponent()).getInputName()).append("' ");
+			call.append("'").append(((IFormSubmittingComponent)getComponent()).getInputName())
+					.append("' ");
 		}
 		else
 		{
@@ -89,9 +127,9 @@
 	@Override
 	protected void onEvent(AjaxRequestTarget target)
 	{
-		// get the form we are really going to submit 
-		final Form form = this.form.getRootForm();
-		
+		// get the form we are really going to submit
+		final Form form = getForm().getRootForm();
+
 		form.onFormSubmitted();
 		if (!form.hasError())
 		{