divanov
divanov

Reputation: 6339

Adding AjaxEventBehavior to form's button prevents form from submitting with Wicket 6.1 and 6.2

I have a simple FormPage derived from WebPage defined like this:

public FormPage() {

    final FeedbackPanel feedback = new FeedbackPanel("feedback");
    add(feedback);

    final TextField<String> entry = new TextField<String>("entry");

    final Button button = new Button("button");
    button.add(new AjaxEventBehavior("onclick") {
        @Override
        protected void onEvent(final AjaxRequestTarget target) {
            System.out.println("Event");
        }
    });

    Form<DataModel> form = new Form<User>("userForm", new CompoundPropertyModel<DataModel>(dataModel)) { 

        @Override
        protected void onValidate() {
            System.out.println("Validate");
            String entryValue = entry.getValue();
            if (entryValue == null || entryValue.length() == 0) {
                error("entry value required");
            }
        };

        @Override
        protected void onSubmit() {
            System.out.println("Submit");
            if (!hasErrors()) {
                String entryValue = entry.getValue();
                if (!entryValue.equals("value")) {
                    error("entry has wrong value");
                }
            }
        };
    };

    form.add(entry);
    form.add(button);
    add(form);
}

I'm trying to do something (in this example just printing to console) on form submission, so I've attached AjaxEventBehavior on button's onclick event. This works perfectly: action is performed on button click, but now the form is not being submitted.

I also was experimenting with

form.add(new AjaxEventBehavior("onsubmit")

and this event handler also prevents form submission. For example,

entry.add(new AjaxEventBehavior("onclick")

allows form to be submitted, but the event is not related to submission. Now I'm puzzled how can I have my form submitted and perform some action on this event.

Upvotes: 3

Views: 12680

Answers (1)

Thomas
Thomas

Reputation: 2261

By default in Wicket 6, a behavior attached to a component prevents the default component action from happening.

If you want to trigger both the behavior and the component action you have to override the updateAjaxRequestAttributes method in your behavior:

@Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
    super.updateAjaxAttributes(attributes);
    attributes.setAllowDefault(true);
}

Upvotes: 11

Related Questions