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 2010/11/30 09:14:21 UTC
svn commit: r1040431 - in /wicket/trunk/wicket/src:
main/java/org/apache/wicket/ajax/form/
main/java/org/apache/wicket/ajax/markup/html/form/
main/java/org/apache/wicket/markup/html/form/
test/java/org/apache/wicket/markup/html/form/
Author: ivaynberg
Date: Tue Nov 30 08:14:20 2010
New Revision: 1040431
URL: http://svn.apache.org/viewvc?rev=1040431&view=rev
Log:
Issue: WICKET-1894
Added:
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/IFormSubmitter.java (with props)
Modified:
wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/form/AjaxFormSubmitBehavior.java
wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/markup/html/form/AjaxButton.java
wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/markup/html/form/AjaxFallbackButton.java
wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/markup/html/form/AjaxSubmitLink.java
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Button.java
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Form.java
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/IFormSubmittingComponent.java
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/StatelessForm.java
wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/SubmitLink.java
wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/FormHasErrorPage.java
Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/form/AjaxFormSubmitBehavior.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/form/AjaxFormSubmitBehavior.java?rev=1040431&r1=1040430&r2=1040431&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/form/AjaxFormSubmitBehavior.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/form/AjaxFormSubmitBehavior.java Tue Nov 30 08:14:20 2010
@@ -17,10 +17,11 @@
package org.apache.wicket.ajax.form;
import org.apache.wicket.Component;
-import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxEventBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
+import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
+import org.apache.wicket.markup.html.form.IFormSubmitter;
import org.apache.wicket.markup.html.form.IFormSubmittingComponent;
import org.apache.wicket.util.string.AppendingStringBuffer;
@@ -45,6 +46,8 @@ public abstract class AjaxFormSubmitBeha
*/
private Form<?> __form;
+ private boolean defaultProcessing = true;
+
/**
* Constructor. This constructor can only be used when the component this behavior is attached
* to is inside a form.
@@ -117,7 +120,10 @@ public abstract class AjaxFormSubmitBeha
final CharSequence url = getCallbackUrl();
AppendingStringBuffer call = new AppendingStringBuffer("wicketSubmitFormById('").append(
- formId).append("', '").append(url).append("', ");
+ formId)
+ .append("', '")
+ .append(url)
+ .append("', ");
if (getComponent() instanceof IFormSubmittingComponent)
{
@@ -137,30 +143,30 @@ public abstract class AjaxFormSubmitBeha
* @see org.apache.wicket.ajax.AjaxEventBehavior#onEvent(org.apache.wicket.ajax.AjaxRequestTarget)
*/
@Override
- protected void onEvent(AjaxRequestTarget target)
+ protected void onEvent(final AjaxRequestTarget target)
{
- getForm().getRootForm().onFormSubmitted();
- if (!getForm().isSubmitted())
- { // only process the form submission if the form was actually submitted -> needs to be
- // enabled and visible
- return;
- }
- if (!getForm().hasError())
- {
- onSubmit(target);
- }
- if (getForm().findParent(Page.class) != null)
+ getForm().getRootForm().onFormSubmitted(new IFormSubmitter()
{
- /*
- * there can be cases when a form is replaced with another component in the onsubmit()
- * handler of this behavior. in that case form no longer has a page and so calling
- * .hasError on it will cause an exception, thus the check above.
- */
- if (getForm().hasError())
+ public Form<?> getForm()
{
- onError(target);
+ return AjaxFormSubmitBehavior.this.getForm();
}
- }
+
+ public boolean getDefaultFormProcessing()
+ {
+ return AjaxFormSubmitBehavior.this.getDefaultProcessing();
+ }
+
+ public void onSubmit()
+ {
+ AjaxFormSubmitBehavior.this.onSubmit(target);
+ }
+
+ public void onError()
+ {
+ AjaxFormSubmitBehavior.this.onError(target);
+ }
+ });
}
/**
@@ -186,4 +192,23 @@ public abstract class AjaxFormSubmitBeha
{
return "return Wicket.$$(this)&&Wicket.$$('" + getForm().getMarkupId() + "')";
}
+
+ /**
+ * @see Button#getDefaultFormProcessing()
+ *
+ * @return {@code true} for default processing
+ */
+ public boolean getDefaultProcessing()
+ {
+ return defaultProcessing;
+ }
+
+ /**
+ * @see Button#setDefaultFormProcessing(boolean)
+ * @param defaultProcessing
+ */
+ public void setDefaultProcessing(boolean defaultProcessing)
+ {
+ this.defaultProcessing = defaultProcessing;
+ }
}
Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/markup/html/form/AjaxButton.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/markup/html/form/AjaxButton.java?rev=1040431&r1=1040430&r2=1040431&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/markup/html/form/AjaxButton.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/markup/html/form/AjaxButton.java Tue Nov 30 08:14:20 2010
@@ -141,6 +141,12 @@ public abstract class AjaxButton extends
{
return AjaxButton.this.getAjaxCallDecorator();
}
+
+ @Override
+ public boolean getDefaultProcessing()
+ {
+ return AjaxButton.this.getDefaultFormProcessing();
+ }
});
}
Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/markup/html/form/AjaxFallbackButton.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/markup/html/form/AjaxFallbackButton.java?rev=1040431&r1=1040430&r2=1040431&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/markup/html/form/AjaxFallbackButton.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/markup/html/form/AjaxFallbackButton.java Tue Nov 30 08:14:20 2010
@@ -91,6 +91,12 @@ public abstract class AjaxFallbackButton
{
return AjaxFallbackButton.this.getAjaxCallDecorator();
}
+
+ @Override
+ public boolean getDefaultProcessing()
+ {
+ return AjaxFallbackButton.this.getDefaultFormProcessing();
+ }
});
}
Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/markup/html/form/AjaxSubmitLink.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/markup/html/form/AjaxSubmitLink.java?rev=1040431&r1=1040430&r2=1040431&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/markup/html/form/AjaxSubmitLink.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/ajax/markup/html/form/AjaxSubmitLink.java Tue Nov 30 08:14:20 2010
@@ -95,6 +95,12 @@ public abstract class AjaxSubmitLink ext
super.onComponentTag(tag);
}
}
+
+ @Override
+ public boolean getDefaultProcessing()
+ {
+ return AjaxSubmitLink.this.getDefaultFormProcessing();
+ }
});
}
@@ -139,6 +145,17 @@ public abstract class AjaxSubmitLink ext
}
/**
+ * Final implementation of the Button's onError. AjaxSubmitLinks have their own onError which is
+ * called.
+ *
+ * @see org.apache.wicket.markup.html.form.Button#onError()
+ */
+ public final void onError()
+ {
+
+ }
+
+ /**
* Listener method invoked on form submit
*
* @param target
Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Button.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Button.java?rev=1040431&r1=1040430&r2=1040431&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Button.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/Button.java Tue Nov 30 08:14:20 2010
@@ -206,4 +206,12 @@ public class Button extends FormComponen
public void onSubmit()
{
}
+
+ /**
+ * @see org.apache.wicket.markup.html.form.IFormSubmittingComponent#onError()
+ */
+ public void onError()
+ {
+
+ }
}
\ No newline at end of file
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=1040431&r1=1040430&r2=1040431&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 Tue Nov 30 08:14:20 2010
@@ -484,7 +484,7 @@ public class Form<T> extends WebMarkupCo
* @return The component which submitted this form, or null if the processing was not triggered
* by a registered IFormSubmittingComponent
*/
- public final IFormSubmittingComponent findSubmittingButton()
+ public final IFormSubmitter findSubmittingButton()
{
IFormSubmittingComponent submittingComponent = getPage().visitChildren(
IFormSubmittingComponent.class, new IVisitor<Component, IFormSubmittingComponent>()
@@ -749,10 +749,27 @@ public class Form<T> extends WebMarkupCo
*
* Handles form submissions.
*
- * @see Form#validate()
+ * @see #onFormSubmitted(IFormSubmitter)
*/
public final void onFormSubmitted()
{
+ onFormSubmitted(null);
+ }
+
+
+ /**
+ * THIS METHOD IS NOT PART OF THE WICKET API. DO NOT ATTEMPT TO OVERRIDE OR CALL IT.
+ *
+ * Handles form submissions.
+ *
+ * @param submitter
+ * listener that will receive form processing events, if {@code null} the form will
+ * attempt to locate one
+ *
+ * @see Form#validate()
+ */
+ public final void onFormSubmitted(IFormSubmitter submitter)
+ {
markFormsSubmitted();
if (handleMultiPart())
@@ -770,14 +787,17 @@ public class Form<T> extends WebMarkupCo
else
{
// First, see if the processing was triggered by a Wicket IFormSubmittingComponent
- final IFormSubmittingComponent submittingComponent = findSubmittingButton();
+ if (submitter == null)
+ {
+ submitter = findSubmittingButton();
+ }
// When processing was triggered by a Wicket IFormSubmittingComponent and that
// component indicates it wants to be called immediately
// (without processing), call IFormSubmittingComponent.onSubmit() right away.
- if (submittingComponent != null && !submittingComponent.getDefaultFormProcessing())
+ if (submitter != null && !submitter.getDefaultFormProcessing())
{
- submittingComponent.onSubmit();
+ submitter.onSubmit();
}
else
{
@@ -785,13 +805,13 @@ public class Form<T> extends WebMarkupCo
Form<?> formToProcess = this;
// find out whether it was a nested form that was submitted
- if (submittingComponent != null)
+ if (submitter != null)
{
- formToProcess = submittingComponent.getForm();
+ formToProcess = submitter.getForm();
}
// process the form for this request
- formToProcess.process(submittingComponent);
+ formToProcess.process(submitter);
}
}
}
@@ -799,7 +819,7 @@ public class Form<T> extends WebMarkupCo
// onError
else if (hasError())
{
- callOnError();
+ callOnError(submitter);
}
}
@@ -817,53 +837,22 @@ public class Form<T> extends WebMarkupCo
* the form has been submitted via the enter key or javascript calling
* form.onsubmit())
*
- * @see #delegateSubmit(IFormSubmittingComponent) for an easy way to process submitting
- * component in the default manner
+ * @see #delegateSubmit(IFormSubmitter) for an easy way to process submitting component in the
+ * default manner
*/
- public void process(IFormSubmittingComponent submittingComponent)
+ public void process(IFormSubmitter submittingComponent)
{
// save the page in case the component is removed during submit
final Page page = getPage();
- // process the form for this request
- if (process())
- {
- // let clients handle further processing
- delegateSubmit(submittingComponent);
- }
-
- // WICKET-1912
- // If the form is stateless page parameters contain all form component
- // values. We need to remove those otherwise they get appended to action URL
- final PageParameters parameters = page.getPageParameters();
- if (parameters != null)
- {
- visitFormComponents(new IVisitor<FormComponent<?>, Void>()
- {
- public void component(final FormComponent<?> formComponent, final IVisit<Void> visit)
- {
- parameters.remove(formComponent.getInputName());
- }
- });
- parameters.remove(getHiddenFieldId());
- }
- }
- /**
- * Process the form.
- * <p>
- * See the class documentation for further details on the form processing
- * </p>
- *
- * @return False if the form had an error
- */
- private boolean process()
- {
if (!isEnabledInHierarchy() || !isVisibleInHierarchy())
{
// since process() can be called outside of the default form workflow, an additional
// check is needed
- return false;
+
+ // FIXME throw listener exception
+ return;
}
// run validation
@@ -876,10 +865,7 @@ public class Form<T> extends WebMarkupCo
markFormComponentsInvalid();
// let subclass handle error
- callOnError();
-
- // Form has an error
- return false;
+ callOnError(submittingComponent);
}
else
{
@@ -893,7 +879,22 @@ public class Form<T> extends WebMarkupCo
updateFormComponentModels();
// Form has no error
- return true;
+ delegateSubmit(submittingComponent);
+ }
+
+ // If the form is stateless page parameters contain all form component
+ // values. We need to remove those otherwise they get appended to action URL
+ final PageParameters parameters = page.getPageParameters();
+ if (parameters != null)
+ {
+ visitFormComponents(new IVisitor<FormComponent<?>, Void>()
+ {
+ public void component(final FormComponent<?> formComponent, final IVisit<Void> visit)
+ {
+ parameters.remove(formComponent.getInputName());
+ }
+ });
+ parameters.remove(getHiddenFieldId());
}
}
@@ -901,8 +902,12 @@ public class Form<T> extends WebMarkupCo
* Calls onError on this {@link Form} and any enabled and visible nested form, if the respective
* {@link Form} actually has errors.
*/
- private void callOnError()
+ private void callOnError(IFormSubmitter submitter)
{
+ if (submitter != null)
+ {
+ submitter.onError();
+ }
onError();
// call onError on nested forms
visitChildren(Form.class, new IVisitor<Component, Void>()
@@ -1254,15 +1259,13 @@ public class Form<T> extends WebMarkupCo
* triggered by something else (like a non-Wicket submit button or a javascript
* execution)
*/
- protected void delegateSubmit(IFormSubmittingComponent submittingComponent)
+ protected void delegateSubmit(IFormSubmitter submittingComponent)
{
// when the given submitting component is not null, it means that it was the
// submitting component
- Form<?> formToProcess = this;
if (submittingComponent != null)
{
// use the form which the submittingComponent has submitted for further processing
- formToProcess = submittingComponent.getForm();
submittingComponent.onSubmit();
}
Added: wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/IFormSubmitter.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/IFormSubmitter.java?rev=1040431&view=auto
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/IFormSubmitter.java (added)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/IFormSubmitter.java Tue Nov 30 08:14:20 2010
@@ -0,0 +1,55 @@
+/*
+ * 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;
+
+
+/**
+ * Triggers a form submit and controls its processing
+ *
+ * @author Igor Vaynberg (ivaynberg)
+ */
+public interface IFormSubmitter
+{
+ /**
+ * Returns the form this component submits.
+ *
+ * @return form submitted by this component
+ */
+ Form<?> getForm();
+
+ /**
+ * Returns whether form should be processed the default way. When false (default is true), all
+ * validation and form updating is bypassed and the onSubmit method of that button is called
+ * directly, and the onSubmit method of the parent form is not called. A common use for this is
+ * to create a cancel button.
+ *
+ * @return defaultFormProcessing
+ */
+ boolean getDefaultFormProcessing();
+
+ /**
+ * Override this method to provide special submit handling in a multi-button form. It is called
+ * whenever the user clicks this particular button.
+ */
+ void onSubmit();
+
+ /**
+ * Method that is invoked when form processing fails; for example, when there are validation
+ * errors.
+ */
+ void onError();
+}
Propchange: wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/IFormSubmitter.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/IFormSubmittingComponent.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/IFormSubmittingComponent.java?rev=1040431&r1=1040430&r2=1040431&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/IFormSubmittingComponent.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/IFormSubmittingComponent.java Tue Nov 30 08:14:20 2010
@@ -23,19 +23,9 @@ import org.apache.wicket.Component;
*
* @author Matej Knopp
*/
-public interface IFormSubmittingComponent
+public interface IFormSubmittingComponent extends IFormSubmitter
{
/**
- * Returns whether form should be processed the default way. When false (default is true), all
- * validation and form updating is bypassed and the onSubmit method of that button is called
- * directly, and the onSubmit method of the parent form is not called. A common use for this is
- * to create a cancel button.
- *
- * @return defaultFormProcessing
- */
- boolean getDefaultFormProcessing();
-
- /**
* Sets the defaultFormProcessing property. When false (default is true), all validation and
* form updating is bypassed and the onSubmit method of that button is called directly, and the
* onSubmit method of the parent form is not called. A common use for this is to create a cancel
@@ -47,22 +37,9 @@ public interface IFormSubmittingComponen
Component setDefaultFormProcessing(boolean defaultFormProcessing);
/**
- * Returns the form this component submits.
- *
- * @return form submitted by this component
- */
- Form<?> getForm();
-
- /**
* Returns the name that is unique to this component, at least within the form.
*
* @return component name
*/
String getInputName();
-
- /**
- * Override this method to provide special submit handling in a multi-button form. It is called
- * whenever the user clicks this particular button.
- */
- void onSubmit();
}
Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/StatelessForm.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/StatelessForm.java?rev=1040431&r1=1040430&r2=1040431&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/StatelessForm.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/StatelessForm.java Tue Nov 30 08:14:20 2010
@@ -59,7 +59,7 @@ public class StatelessForm<T> extends Fo
* @see org.apache.wicket.markup.html.form.Form#process(org.apache.wicket.markup.html.form.IFormSubmittingComponent)
*/
@Override
- public void process(IFormSubmittingComponent submittingComponent)
+ public void process(IFormSubmitter submittingComponent)
{
// set redirect to true for a stateless form.
// TODO: NG
Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/SubmitLink.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/SubmitLink.java?rev=1040431&r1=1040430&r2=1040431&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/SubmitLink.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/form/SubmitLink.java Tue Nov 30 08:14:20 2010
@@ -192,7 +192,7 @@ public class SubmitLink extends Abstract
{
// find the root form - the one we are really going to submit
Form<?> root = getForm().getRootForm();
- StringBuilder sb = new StringBuilder(100);
+ StringBuilder sb = new StringBuilder(100);
sb.append("var e=document.getElementById('");
sb.append(root.getHiddenFieldId());
sb.append("'); e.name=\'");
@@ -231,4 +231,11 @@ public class SubmitLink extends Abstract
{
}
+ /**
+ * @see org.apache.wicket.markup.html.form.IFormSubmittingComponent#onError()
+ */
+ public void onError()
+ {
+ }
+
}
\ No newline at end of file
Modified: wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/FormHasErrorPage.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/FormHasErrorPage.java?rev=1040431&r1=1040430&r2=1040431&view=diff
==============================================================================
--- wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/FormHasErrorPage.java (original)
+++ wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/form/FormHasErrorPage.java Tue Nov 30 08:14:20 2010
@@ -83,7 +83,7 @@ public class FormHasErrorPage extends We
}
@Override
- public void process(IFormSubmittingComponent submittingComponent)
+ public void process(IFormSubmitter submittingComponent)
{
// set the error based on which link submitted the form
if (submittingComponent == submitFormComponent)