我正在使用BeanValidation for Form input和AjaxFallbackButton来提交表单。和反馈面板,用于显示错误。当我输入无效时,表单不会提交,但反馈面板没有显示。
onError, form.getFeedbackMessages() 返回空数组。
检票口版本 6.18.0。
这是代码:
Form<Address> form = getForm();
add(form);
FeedbackPanel feedbackPanel = new FeedbackPanel("feedbackMessage");
feedbackPanel.setOutputMarkupId(true);
add(feedbackPanel);
public Form<Address> getForm() {
CompoundPropertyModel<Address> model = new CompoundPropertyModel<Address>(address);
final Form<Address> form = new Form<Address>("addressForm", model);
form.add(new Label("fNameLabel", new ResourceModel("fNameLabel")));
form.add(new Label("lNameLabel", new ResourceModel("lNameLabel")));
form.add(new Label("workLabel", new ResourceModel("workLabel")));
form.add(new Label("homeLabel", new ResourceModel("homeLabel")));
form.add(new TextField<String>("firstName").add(new PropertyValidator<String>()));
form.add(new TextField<String>("lastName").add(new PropertyValidator<String>()));
form.add(new TextField<String>("homeLocation").add(new PropertyValidator<String>()));
form.add(new TextField<String>("workLocation").add(new PropertyValidator<String>()));
form.add(new AjaxFallbackButton("submit", form) {
/**
*
*/
private static final long serialVersionUID = 6672729206839722437L;
@Override
protected void onError(final AjaxRequestTarget target, final Form form) {
Page page = target.getPage();
for (Component component : page.visitChildren()) {
String markupId = component.getMarkupId();
if (markupId.contains("feedbackMessage")) {
if (form.hasFeedbackMessage()) {
System.out.println(form.getFeedbackMessages());
}
}
}
}
@Override
protected void onSubmit(AjaxRequestTarget target, Form form) {
if (address.getFirstName() != null) {
AddressGenerator.getInstance().add(address);
modalWindow.closeCurrent(target);
}
}
});
return form;
}
表单在ModalWindow中。
Component#getFeedbackMessages()
仅返回此组件实例的消息。它不去看望孩子!
使用以下方法更新onError()
方法:
@Override
protected void onError(final AjaxRequestTarget target, final Form form) {
target.add(feedbackPanel);
}
您可以使用if (form.hasError())
但由于您处于AjaxFallbackButton#onError()
这意味着存在错误。