验证错误时重置表单字段
我有一个相当复杂的表单,因为表单字段的数量是灵活的。简而言之,模型对象是一个 TLabel (TranslationLabel),其中包含值(翻译)的 Map。这里的语言是一个枚举,因此其想法是给出翻译的字段(文本区域)的数量取决于该枚举中的值。
这是我的表单(简化):
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);
}
}
现在,它工作正常,为每种语言创建 1 个文本区域,并且其值也设置得很好;更重要的是,当更改时,模型会按预期更新。
如果您在清空文本区域后提交表单(因此最初有一个值),那么当然会出现验证错误(必需)。正常(检票口)行为是无效字段仍然为空,但由于某种原因原始值被重置,我不明白为什么。
如果我像这样覆盖onError:
@Override
protected void onError() {
this.updateFormComponentModels();
}
那么就可以了,该字段的值设置为提交的值(空)而不是原始值。
知道是什么原因造成的吗?由于我设置表单的方式,检票口未能执行什么操作(因为使用简单的表单/模型,它可以正常工作,就像检票口示例一样)?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为答案发布,因此问题可以标记为已解决:
ListView 确实在渲染时重新创建其所有项目。这意味着验证将被破坏。查看 ListView
调用 的 API 文档 ListView 上的 setReuseItems() 解决了这个问题。
问候,
伯特
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