سؤال

I have a rather complex form in the way that the number of form fields is flexibel. In short, the model object is a TLabel (TranslationLabel) that contains a Map of values (translations). Language here is an enum so the idea is that the number of fields (text areas) for which a translation is given depends on the values in this enum.

This is my form (simplified):

public class TranslationEditForm extends Form {

private final static List<Language> LANGUAGES = newArrayList(Language.values());

public TranslationEditForm(String id, final TranslationLabelView label) {
    super(id, new CompoundPropertyModel<TranslationLabelView>(label));

    ListView<Language> textAreas = new ListView<Language>("translationRepeater", LANGUAGES) {

        @Override
        protected void populateItem(final ListItem<Language> itemLang) {
            //loop through the languages and create 1 textarea per language
            itemLang.add(new Label("language", itemLang.getModelObject().toString()));
            Model<String> textModel = new Model<String>() {

                @Override
                public String getObject() {
                    //return the value for current language
                    return label.getValue(itemLang.getModelObject());
                }

                @Override
                public void setObject(String object) {
                    //set the value for current language
                    label.getTranslations().put(itemLang.getModelObject(), object);
                }
            };
            itemLang.add(new TextArea<String>("value", textModel).setRequired(true));
        }
    };
    //add the repeater containing a textarea per language to the form
    this.add(textAreas);
}
}

Now, it works fine, 1 text area is created per language and its value is also set nicely; even more when changed the model gets updated as intended.

If you submit the form after emptying a text area (so originally there was a value) then of course there is a validation error (required). Normal (wicket) behaviour would be that the invalid field is still empty but for some reason the original value is reset and I don't understand why.

If I override onError like this:

@Override
protected void onError() {
    this.updateFormComponentModels();
}

then it is fine, the value of the field is set to the submitted value (empty) instead of the original value.

Any idea what is causing this? What is wicket failing to do because the way I've set up the form (because with a simple form/model this is working fine as are the wicket examples)?

هل كانت مفيدة؟

المحلول

Posted as answer, so the question can be marked as solved:

ListView does recreate all its items at render time. This means that the validation will be broken. Have a look at API doc of the ListView

Calling setReuseItems() on the ListView solves this.

Regards, Bert

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top