3

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

Joachim Rohde
  • 5,915
  • 2
  • 29
  • 46
topriddy
  • 71
  • 5
  • 1
    Can you add an onError() to your Form and see if you receive an error in form processing? From the snippet above, it looks good so far. – bert Nov 29 '13 at 12:05
  • hi bert, it always hits my onSubmit() method but then shows a log of the first selected option. I suspect there might be an issue with how DropDownChoice works with PropertyModels. Like why do i have to set the reference to null? I am referring to the State/LGA switch classic example now. – topriddy Nov 29 '13 at 12:23
  • You have an entity ("empowerment") field on the page for the PropertyModel ("this" == page)? – mrak Nov 29 '13 at 13:00
  • I have an entity Empowerment empowerment on the Form itself as a member variable. – topriddy Nov 29 '13 at 20:58

2 Answers2

4

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.

topriddy
  • 71
  • 5
3

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
    }
});
Robert Niestroj
  • 15,299
  • 14
  • 76
  • 119