topriddy
topriddy

Reputation: 71

Wicket DropDownChoice NOT working correctly with PropertyModels

I have been trying to debug why my DropDownChoice in a simple form with just the DropDown and a Submit Button hasn't been working correctly for hours now.

It has a very weird behaviour. Where the first value selected in the drop down choice is sent successfully to the server after which any other choice select is not updated by the model. ie if I have a List persons, and I select the 2nd person, it submits this successfully. However, on select of another person and trying to submit it again it keeps showing the first selected option.

Snippets of code here:

 ChoiceRenderer<Empowerment> empowermentChoiceRenderer = new ChoiceRenderer<>("name", "id");
 final DropDownChoice<Empowerment> empowermentDropDownChoice =
                    new DropDownChoice<>("empowerment", new PropertyModel<Empowerment>(this, "empowerment"), empowermentList, empowermentChoiceRenderer);
 empowermentDropDownChoice.setRequired(true);
 add(empowermentDropDownChoice);

Only way I am able to get a decent behaviour is if I set the empowerment variable above to null. In this case, on submit the empowerment is reinitialised to null and then a new submit works correctly.

empowerment is just a JPA entity.

I'll be glad to know if this is a known issue. I experienced it in wicket 6.9.1 and wicket 6.12

Upvotes: 3

Views: 2245

Answers (2)

topriddy
topriddy

Reputation: 71

Finally, found the solution to the problem. Above code is correct, but problem lied in the entity class itself - Empowerment needs to implement Equals and Hashcode correctly.

The DropDownChoice works just fine after this.

Upvotes: 4

Robert Niestroj
Robert Niestroj

Reputation: 16131

Add an OnChangeAjaxBehavior to your DropDownChoice. This will update the model value on every selection change you make on the drop down:

empowermentDropDownChoice .add(new OnChangeAjaxBehavior() {

    @Override
    protected void onUpdate(AjaxRequestTarget art) {
        //just model update
    }
});

Upvotes: 3

Related Questions