2

We have inherited a large code base that uses Wicket 6 where we have a RadioGroup of preferred contact type choices (SMS, e-mail, etc). When a Radio for SMS is selected, a TextField for phone number is made visible, same for e-mail and so on.

This has been implemented by adding an AjaxEventBehavior for "onclick" event to each Radio. The onEvent(AjaxRequestTarget) method calls RadioGroup.onSelectionChanged() and updates the visibility of each TextField:

radioSms = new Radio<>("sms", ...);
radioEmail = new Radio<>("email", ...);
radioGroup = new RadioGroup<>("contactGroup");
radioGroup.add(radioSms)
          .add(radioEmail)
          .add(textFieldSms)
          .add(textFieldEmail);
radioSms.add(new OnClickEventBehavior());
radioEmail.add(new OnClickEventBehavior());

...

private class OnClickEventBehavior extends AjaxEventBehavior {
    protected OnClickEventBehavior() {
        super("onclick");
    }

    @Override
    protected void onEvent(AjaxRequestTarget target) {
        radioGroup.onSelectionChanged();
        updateTextFieldVisibilities();
        target.add(form);
    }
}

Our problems are that we have to upgrade to Wicket 8, the onSelectionChanged() method has been removed from RadioGroup and we can not find any documentation about a possible replacement. From reading between the lines of Wicket 6 JavaDocs, I get the feeling that the onSelectionChanged() method shouldn't even be called manually, since the docs only state "Called when a new option is selected." in a passive form.

I have questions:

  1. Did our ancestors abuse the Wicket API by calling onSelectionChanged() manually?
  2. Is there a replacement for RadioGroup.onSelectionChanged() in Wicket 8?
  3. What is the correct way of implementing the functionality described in the first paragraph?

1 Answers1

1

You need to consult with the migration page at https://cwiki.apache.org/confluence/x/E7OnAw

The new way is:

// Wicket 8.x
new CheckBox("id", model).add(new FormComponentUpdatingBehavior() {
    protected void onUpdate() {
        // do something, page will be rerendered;
    }

    protected void onError(RuntimeException ex) {
        super.onError(ex);
    }
});
martin-g
  • 17,243
  • 2
  • 23
  • 35
  • Thanks! I had that open in my browser but I was confused as our code calls the onSelectionChanged() of the RadioGroup and the "old way" in the migration guide did not resemble our code at all. Do you possibly have any insight on the first question? – AB - Work Account Mar 05 '20 at 11:04
  • I never needed to call manually `onSelectionChanged()` myself, but maybe in some cases it is OK. There are two other related methods in Wicket: `onModelChanging()` and `onModelChanged()` which I have used. – martin-g Mar 05 '20 at 14:17