[英]Apache wicket: how to update model after validation error
我有使用dateTimeField和ListView的表單。 ListView看起來像這樣:
final ListView<String> countryView = new ListView<String>("country", model.<List<String>>bind("country")) {
@Override
protected void populateItem(final ListItem<String> item) {
final String country = item.getModelObject();
item.add(new ValidationDisplayableLabel("country", country, new String[] { modelPath }));
item.add(new AjaxLink("deleteLink") {
@Override
public void onClick(AjaxRequestTarget target) {
model.getObject().getCountry().remove(country);
if (issPeriod) {
addButton.setVisible(true);
countryTextField.setVisible(true);
findButton.setVisible(true);
}
if (target != null)
target.addComponent(rowPanel);
}
});
}
};
countryTextField = new ValidationDisplayableTextField("countryCodeInput", model.bind("oneCountry"), "job.country.value");
**countryView.setReuseItems(true);**
rowPanel.add(countryView);
rowPanel.add(countryTextField);
addButton.setOutputMarkupPlaceholderTag(true);
rowPanel.add(addButton);
addButton看起來像這樣:
AjaxSubmitLink addButton = new AjaxSubmitLink(LinkNames.addCountry.toString()) {
@Override
public void onSubmit(AjaxRequestTarget target, Form form) {
if (model.getObject().getOneCountry() != null)
addCountry();
if (target != null)
target.addComponent(rowPanel);
target.addComponent(form.getPage().get("feedbackPanel"));
}
@Override
protected void onError(AjaxRequestTarget target, Form<?> form)
{
onSubmit(target, form);
}
};
問題是,當我的dateTimeField失敗時(例如設置小時為100),在countryTextField中輸入國家代碼,然后按addButton,它會在反饋面板中顯示驗證消息,小時范圍不正確,但不添加國家/地區。 這是因為我的模型沒有更新。 也許有辦法手動更新它? 因此將顯示驗證消息,但國家listView仍然可以更新?
整個表單的提交是在其他按鈕上,所以從邏輯上講,即使dateTimeField中存在驗證錯誤,添加國家也是正常的。
謝謝!
PS我已經閱讀了很多關於類似問題的帖子,但是大多數帖子都是用.setReuseItems(true)解決的,但它在我的情況下不起作用。
PPS Apache wicket 1.4.17
作為此答案的更新,在Wicket 6中,您可以通過覆蓋表單中的onError()來實現此目的:
@Override
protected void onError() {
super.onError();
this.updateFormComponentModels();
}
我在項目中遇到了類似的問題,我找到的解決方法是使用一個特殊的訪問者。 即使提交的輸入無效,它也會更新模型。
public class VisitorUpdateModelWithoutValidation implements FormComponent.IVisitor {
public Object formComponent(IFormVisitorParticipant formComponent) {
if (formComponent instanceof FormComponent) {
final FormComponent<?> formComponent1 = (FormComponent<?>) formComponent;
boolean required = formComponent1.isRequired();
if (required) {
formComponent1.setRequired(false);
}
formComponent1.modelChanging();
formComponent1.validate();
formComponent1.updateModel();
formComponent1.modelChanged();
if (required) {
formComponent1.setRequired(true);
}
}
return Component.IVisitor.CONTINUE_TRAVERSAL;
}
}
只需在行為的onSubmit
方法中使用它: getForm().visitFormComponents(new VisitorUpdateModelWithoutValidation());
在定位更新之前,可以在要更新的字段上發出field.clearInput()
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.