You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by mg...@apache.org on 2013/05/23 16:39:44 UTC
[5/5] git commit: WICKET-5196 Make AjaxFormValidatingBehavior
non-static
WICKET-5196 Make AjaxFormValidatingBehavior non-static
Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/a6a45f29
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/a6a45f29
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/a6a45f29
Branch: refs/heads/master
Commit: a6a45f29743294f2749ff0d72c22cf9f60dbb65c
Parents: 437e01d
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
Authored: Thu May 23 11:25:31 2013 +0300
Committer: Martin Tzvetanov Grigorov <mg...@apache.org>
Committed: Thu May 23 17:39:31 2013 +0300
----------------------------------------------------------------------
.../ajax/form/AjaxFormValidatingBehavior.java | 145 ++++++++++-----
.../src/test/java/org/apache/wicket/RedirectB.java | 2 +-
.../wicket/util/tester/MockAjaxFormPage.java | 24 ++-
.../wicket/util/tester/WicketTesterTest.java | 2 +-
.../wicket/examples/ajax/builtin/FormPage.java | 4 +-
5 files changed, 118 insertions(+), 59 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/wicket/blob/a6a45f29/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormValidatingBehavior.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormValidatingBehavior.java b/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormValidatingBehavior.java
index 629976c..1afcc68 100644
--- a/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormValidatingBehavior.java
+++ b/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormValidatingBehavior.java
@@ -17,9 +17,11 @@
package org.apache.wicket.ajax.form;
import org.apache.wicket.Component;
+import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
import org.apache.wicket.ajax.attributes.ThrottlingSettings;
+import org.apache.wicket.behavior.Behavior;
import org.apache.wicket.feedback.IFeedback;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
@@ -37,38 +39,90 @@ import org.apache.wicket.util.visit.IVisitor;
* @author Igor Vaynberg (ivaynberg)
*
*/
-public class AjaxFormValidatingBehavior extends AjaxFormSubmitBehavior
+public class AjaxFormValidatingBehavior extends Behavior
{
private static final long serialVersionUID = 1L;
+ private final String event;
+ private final Duration throttleDelay;
+
+ /**
+ * The form that will be submitted via ajax
+ */
+ private Form<?> form;
+
+ /**
+ * A flag indicating whether this behavior has been rendered at least once
+ */
+ private boolean hasBeenRendered = false;
+
/**
* Construct.
*
- * @param form
- * form that will be submitted via ajax
* @param event
* javascript event this behavior will be invoked on, like onclick
*/
- public AjaxFormValidatingBehavior(Form<?> form, String event)
+ public AjaxFormValidatingBehavior(String event)
{
- super(form, event);
+ this(event, null);
}
/**
- *
- * @see org.apache.wicket.ajax.form.AjaxFormSubmitBehavior#onSubmit(org.apache.wicket.ajax.AjaxRequestTarget)
+ * Construct.
+ *
+ * @param event
+ * javascript event this behavior will be invoked on, like onclick
+ * @param throttleDelay
+ * the duration for which the Ajax call should be throttled
*/
+ public AjaxFormValidatingBehavior(String event, Duration throttleDelay)
+ {
+ this.event = event;
+ this.throttleDelay = throttleDelay;
+ }
+
@Override
+ public void bind(Component component)
+ {
+ super.bind(component);
+
+ if (component instanceof Form<?>)
+ {
+ form = (Form<?>) component;
+ }
+ else
+ {
+ form = Form.findForm(component);
+ if (form == null)
+ {
+ throw new WicketRuntimeException(AjaxFormValidatingBehavior.class.getSimpleName() +
+ " should be bound to a Form component or a component that is inside a form!");
+ }
+ }
+ }
+
+ @Override
+ public void onConfigure(Component component)
+ {
+ super.onConfigure(component);
+
+ if (hasBeenRendered == false)
+ {
+ hasBeenRendered = true;
+
+ form.visitChildren(FormComponent.class, new FormValidateVisitor());
+ }
+ }
+
protected void onSubmit(final AjaxRequestTarget target)
{
addFeedbackPanels(target);
}
- /**
- *
- * @see org.apache.wicket.ajax.form.AjaxFormSubmitBehavior#onError(org.apache.wicket.ajax.AjaxRequestTarget)
- */
- @Override
+ protected void onAfterSubmit(final AjaxRequestTarget target)
+ {
+ }
+
protected void onError(AjaxRequestTarget target)
{
addFeedbackPanels(target);
@@ -79,9 +133,9 @@ public class AjaxFormValidatingBehavior extends AjaxFormSubmitBehavior
*
* @param target
*/
- private void addFeedbackPanels(final AjaxRequestTarget target)
+ protected final void addFeedbackPanels(final AjaxRequestTarget target)
{
- getComponent().getPage().visitChildren(IFeedback.class, new IVisitor<Component, Void>()
+ form.getPage().visitChildren(IFeedback.class, new IVisitor<Component, Void>()
{
@Override
public void component(final Component component, final IVisit<Void> visit)
@@ -91,47 +145,16 @@ public class AjaxFormValidatingBehavior extends AjaxFormSubmitBehavior
});
}
- /**
- * Adds this behavior to all form components of the specified form
- *
- * @param form
- * @param event
- */
- public static void addToAllFormComponents(final Form<?> form, final String event)
- {
- addToAllFormComponents(form, event, null);
- }
-
- /**
- * Adds this behavior to all form components of the specified form
- *
- * @param form
- * @param event
- * @param throttleDelay
- */
- public static void addToAllFormComponents(final Form<?> form, final String event,
- final Duration throttleDelay)
+ protected void updateAjaxAttributes(final AjaxRequestAttributes attributes)
{
- form.visitChildren(FormComponent.class, new FormValidateVisitor(form, event, throttleDelay));
}
- private static class FormValidateVisitor implements IVisitor<Component, Void>, IClusterable
+ private class FormValidateVisitor implements IVisitor<FormComponent, Void>, IClusterable
{
- private final Form<?> form;
- private final String event;
- private final Duration throttleDelay;
-
- private FormValidateVisitor(Form<?> form, String event, Duration throttleDelay)
- {
- this.form = form;
- this.event = event;
- this.throttleDelay = throttleDelay;
- }
-
@Override
- public void component(final Component component, final IVisit<Void> visit)
+ public void component(final FormComponent component, final IVisit<Void> visit)
{
- final AjaxFormValidatingBehavior behavior = new AjaxFormValidatingBehavior(form, event)
+ final AjaxFormSubmitBehavior behavior = new AjaxFormSubmitBehavior(form, event)
{
@Override
protected void updateAjaxAttributes(final AjaxRequestAttributes attributes)
@@ -144,7 +167,31 @@ public class AjaxFormValidatingBehavior extends AjaxFormSubmitBehavior
ThrottlingSettings throttlingSettings = new ThrottlingSettings(id,
throttleDelay);
attributes.setThrottlingSettings(throttlingSettings);
+ attributes.setPreventDefault(false); // WICKET-5194
}
+
+ AjaxFormValidatingBehavior.this.updateAjaxAttributes(attributes);
+ }
+
+ @Override
+ protected void onSubmit(AjaxRequestTarget target)
+ {
+ super.onSubmit(target);
+ AjaxFormValidatingBehavior.this.onSubmit(target);
+ }
+
+ @Override
+ protected void onAfterSubmit(AjaxRequestTarget target)
+ {
+ super.onAfterSubmit(target);
+ AjaxFormValidatingBehavior.this.onAfterSubmit(target);
+ }
+
+ @Override
+ protected void onError(AjaxRequestTarget target)
+ {
+ super.onError(target);
+ AjaxFormValidatingBehavior.this.onError(target);
}
};
component.add(behavior);
http://git-wip-us.apache.org/repos/asf/wicket/blob/a6a45f29/wicket-core/src/test/java/org/apache/wicket/RedirectB.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/java/org/apache/wicket/RedirectB.java b/wicket-core/src/test/java/org/apache/wicket/RedirectB.java
index a990217..704e620 100644
--- a/wicket-core/src/test/java/org/apache/wicket/RedirectB.java
+++ b/wicket-core/src/test/java/org/apache/wicket/RedirectB.java
@@ -34,7 +34,7 @@ public class RedirectB extends WebPage
*/
public RedirectB()
{
- add(new Label("interceptContinuationURL", new Model<String>(InterceptData.get()
+ add(new Label("interceptContinuationURL", Model .of(InterceptData.get()
.getOriginalUrl()
.toString())));
http://git-wip-us.apache.org/repos/asf/wicket/blob/a6a45f29/wicket-core/src/test/java/org/apache/wicket/util/tester/MockAjaxFormPage.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/java/org/apache/wicket/util/tester/MockAjaxFormPage.java b/wicket-core/src/test/java/org/apache/wicket/util/tester/MockAjaxFormPage.java
index 5898379..eeaf101 100644
--- a/wicket-core/src/test/java/org/apache/wicket/util/tester/MockAjaxFormPage.java
+++ b/wicket-core/src/test/java/org/apache/wicket/util/tester/MockAjaxFormPage.java
@@ -69,28 +69,40 @@ public class MockAjaxFormPage extends WebPage
*/
public MockAjaxFormPage()
{
- Form<MockDomainObject> form = new Form<MockDomainObject>("form",
- new CompoundPropertyModel<MockDomainObject>(new MockDomainObject()));
+ Form<MockDomainObject> form = new Form<>("form",
+ new CompoundPropertyModel<>(new MockDomainObject()));
add(form);
final Button submit = new Button("submit");
submit.setOutputMarkupId(true);
submit.setEnabled(false);
form.add(submit);
- final TextField<String> text = new TextField<String>("text");
+ final TextField<String> text = new TextField<>("text");
+ form.add(text);
text.setRequired(true);
text.add(new StringValidator(4, null));
- text.add(new AjaxFormValidatingBehavior(form, "onkeyup")
+ text.add(new AjaxFormValidatingBehavior("keyup")
{
private static final long serialVersionUID = 1L;
@Override
- protected void onEvent(AjaxRequestTarget target)
+ protected void onSubmit(AjaxRequestTarget target)
{
+ super.onSubmit(target);
+
+ text.validate();
+ submit.setEnabled(text.isValid());
+ target.add(submit);
+ }
+
+ @Override
+ protected void onError(AjaxRequestTarget target)
+ {
+ super.onError(target);
+
text.validate();
submit.setEnabled(text.isValid());
target.add(submit);
}
});
- form.add(text);
}
}
http://git-wip-us.apache.org/repos/asf/wicket/blob/a6a45f29/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java b/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
index 2014c94..fb4cb38 100644
--- a/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
+++ b/wicket-core/src/test/java/org/apache/wicket/util/tester/WicketTesterTest.java
@@ -914,7 +914,7 @@ public class WicketTesterTest extends WicketTestCase
private void setTextFieldAndAssertSubmit(boolean expected)
{
- tester.executeAjaxEvent("form:text", "onkeyup");
+ tester.executeAjaxEvent("form:text", "keyup");
Button submit = getSubmitButton();
// System.out.println(Session.get().getFeedbackMessages());
assertEquals(expected, submit.isEnabled());
http://git-wip-us.apache.org/repos/asf/wicket/blob/a6a45f29/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FormPage.java
----------------------------------------------------------------------
diff --git a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FormPage.java b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FormPage.java
index 4f68189..6c85e80 100644
--- a/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FormPage.java
+++ b/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/FormPage.java
@@ -52,7 +52,7 @@ public class FormPage extends BasePage
// add form with markup id setter so it can be updated via ajax
Bean bean = new Bean();
- Form<Bean> form = new Form<Bean>("form", new CompoundPropertyModel<Bean>(bean));
+ Form<Bean> form = new Form<>("form", new CompoundPropertyModel<>(bean));
add(form);
form.setOutputMarkupId(true);
@@ -77,7 +77,7 @@ public class FormPage extends BasePage
// attach an ajax validation behavior to all form component's keydown
// event and throttle it down to once per second
- AjaxFormValidatingBehavior.addToAllFormComponents(form, "keydown", Duration.ONE_SECOND);
+ form.add(new AjaxFormValidatingBehavior("keydown", Duration.ONE_SECOND));
// add a button that can be used to submit the form via ajax
form.add(new AjaxButton("ajax-button", form)