2

Please consider the following signup:

<form wicket:id="form">
  <div wicket:id="fooList">                                        
    <input wicket:id="fooList.quxField" type="text" size="10"/>
  </div>                
  <button wicket:id="submit"><wicket:message key="submitText"/></button>    
</form>

And these two classes (I am assuming setters, getters etc.)

class FooClazz {        
    String quxField;
}

class BarClazz {
    List<FooClazz> fooList;
}

And this form (models are wrapped in CompoundPropertyModel):

class BarForm extends Form<BarClazz> {
  public BarForm(String id,final IModel<BarClazz> model){
    super(id,model);
    add(new ListView<FooClazz>("fooList"){
      @Override
      protected void populateItem(final ListItem<FooClazz> item){
        item.add(new TextField<String>("fooList.quxField"));
      }                   
    }
  }
}

Now the above code is generating a runtime exception for me:

2011-12-11 16:33:46 ERROR [org.apache.wicket.DefaultExceptionMapper] Unexpected error occurred org.apache.wicket.WicketRuntimeException: The expression 'quxField' is neither an index nor is it a method or field for the list class java.util.ArrayList

I can change the TextField to include a Model like this:

item.add(new TextField<String>("fooList.quxField", new Model<String>(model.getObject().getFooList().getQuxField())));

This resolves the error, but when I submit the form (with an Ajaxbutton) I never get to see the values entered into the form fields.

So how can I keep the TextField models connected to my form model? Am I overlooking the obvious? (This is of course just a very shortened version of my original code ...)

Need to add: all my models are wrapped in CompoundPropertyModels.

Thanks in advance for any tips how to fix this.

Yashima
  • 2,248
  • 2
  • 23
  • 39

1 Answers1

5

I found it. I need to include a model for the TextField that has implementations for both getObject() and of course setObject(). So I really was missing the obvious.

@Override
protected void populateItem(final ListItem<Taste> item) {
    final TextField<String> quxField = new TextField<String>("tastes.quxField", new Model<String>() {
        @Override
        public String getObject() {
            return item.getModel().getObject().getquxField();
        }

        @Override
        public void setObject(final String value) {
            item.getModel().getObject().setquxField(value);
        }
    });
    item.add(quxField);
}
Yashima
  • 2,248
  • 2
  • 23
  • 39
  • 1
    Nice that you found a solution. Another option is to use "quxField" as wicket:id and use a PropertyModel for the textField. Also, mark your answer as resolved. – bert Dec 12 '11 at 09:58
  • Thanks for the hint that's also a good one (I will mark question as resolved as soon as stackoverflow lets me do it of course). – Yashima Dec 12 '11 at 17:43