javaajaxwicketwicket-7

Wicket form changes removed on ajax update


I have a pretty big and complex form which contains lots of form components.

On one of the drop down fields I have added a AjaxFormComponentUpdatingBehavior to handle changes in the drop down. Based on these changes I am updating some other fields in the form, this way:

masterFooDropDown.add(new AjaxFormComponentUpdatingBehavior("change") {
    @Override
    protected void onUpdate(AjaxRequestTarget target) {
        String value = (String) getFormComponent().getModelObject();
        if (value != null) {
            for (Foo foo: form.getModelObject().getFoos()) {
                foo.setValue(value);
            }

            target.add(form);
        }
    }
});

The Foo fields gets updated correctly with the value from the drop down.

The problem is that any changes in the other text fields in the form is removed. I understand that this happens, since they have not been updated in the model.

How can I solve this? Can I make all the filled in data in the form so far be written to the model in some way (without submitting the form)?

I need to add the whole form to the target in the ajax method since the fields that should be updated is children of the form model object and added to the form dynamically. For example, I cannot do target.add(fooFieldX) since there might be any number of "fooFields".


Solution

    1. You can use AjaxFormComponentUpdatingBehavior/AjaxFormChoiceComponentUpdatingBehavior on all other form (choice) components so that they update the form's model object when modified.

    2. You can use Component#visitChildren() and dynamically decide which particular form components to add to the AjaxRequestTarget, if it is easy to decide this. Even AjaxRequestTarget has method #addChildren() but it is not that flexible to filter the children components.