0

我正在使用带有 AjaxFallbackButton 的表单输入的 BeanValidation 来提交表单。和用于显示错误的反馈面板。当我输入无效输入时,表单不会提交,但没有显示反馈面板。

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;
}

表单在模态窗口中。

4

1 回答 1

0

Component#getFeedbackMessages()返回此组件实例的消息。它不拜访孩子!

更新您的onError()方法:

    @Override
    protected void onError(final AjaxRequestTarget target, final Form form) {   
        target.add(feedbackPanel);
    }

您可以使用if (form.hasError()),但既然您在其中,AjaxFallbackButton#onError()则意味着存在错误。

于 2017-01-04T14:12:24.810 回答