0

I have a DropDownChoice with two OnChangeAjaxBehaviors on it. When i select the value 2 which should set to DropDownChoice disabled it get's for a second disable before it shows me the AccessDeniedPage and in the server logs i see a ListenerNotAllowedInvocationException. Have this in Wicket 6 and 7.

Any idea how to fix this?

Code below:

private Integer selected;

public HomePage(final PageParameters parameters) {
    super(parameters);
    final DropDownChoice<Integer> ddc = new DropDownChoice<Integer>("ddc", new PropertyModel(this, "selected"), Arrays.asList(1,2,3)){
        @Override
        protected void onConfigure() {
            super.onConfigure(); 
            setEnabled(!Objects.equals(getModel().getObject(), 2));
        }

    };
    ddc.add(new OnChangeAjaxBehavior() {
        @Override
        protected void onUpdate(AjaxRequestTarget art) {
            art.add(getComponent()); 
            saveToDb(model.getObject);
        }
    });
    ddc.add(new OnChangeAjaxBehavior() {
        @Override
        protected void onUpdate(AjaxRequestTarget art) {
            art.add(getComponent(), otherComponent);
        }

    });
    ddc.setOutputMarkupId(true);
    add(ddc);
}

I have tried disabling one of the behaviors with the same condition as the component, but i did not work.

    @Override
    public boolean isEnabled(Component component) {
        return !Objects.equals(component.getDefaultModelObject(), 2);
    }

Or like this:

    @Override
    public boolean isEnabled(Component component) {
        return component.isEnabled();
    }
Robert Niestroj
  • 15,299
  • 14
  • 76
  • 119

1 Answers1

0

The problem is the following:

  • the first Ajax call updates the model object to 2 and re-renders the DropDownChoice as disabled
  • then the second Ajax call is executed and Wicket prevents the update because the component is disabled

Why do you need to use 2 OnChangeAjaxBehaviors on the same DropDownChoice ?

One solution is to prevent such second Ajax call by using AjaxChannel.ACTIVE on both Ajax behaviors. This will tell Wicket JS to not execute the second Ajax call if there is an active Ajax call on this ajax channel.

Update: Another approach would be to override Behavior#isEnabled() and check whether there is a request parameter with name ddChoice#getInputName() and its value is 2. If this is the case then return true, otherwise call super.isEnabled().

martin-g
  • 17,243
  • 2
  • 23
  • 35
  • I need two because: one is attached to every component in a form to save values to DB (online save). The second one is for refreshing certain components on the form based on the selected value in this certain dropdownchoice. The first one is generic for all components so i cannot insert there the custom logic from the second behavior. So i need really both executed. I've updated the code a little bit. – Robert Niestroj Jul 18 '18 at 08:26