阿帕奇检票口:如何更新验证错误后,模型 [英] Apache wicket: how to update model after validation error

查看:125
本文介绍了阿帕奇检票口:如何更新验证错误后,模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有形式DateTimeField字段,和ListView。
的ListView看起来像:

I have form with dateTimeField, and ListView. ListView looks like that:

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

和Add按钮看起来像:

And the addButton looks like that:

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进入国家code,并在Add按钮preSS,它反馈面板显示验证消息,这一个小时的范围是不正确,但不添加国家。这是因为我的模型不更新。也许有一种方法来手动更新呢?因此,将显示验证消息,但该国的ListView仍然可以更新?

The thing is, that when I fail my dateTimeField (e.g. set hours to 100), enter country code in countryTextField, and press on addButton, it displays validation message in feedback panel, that hour range is incorrect, but don't add the country. This is because my model isn't updated. Maybe there is a way to update it manually? So validation message will be displayed, but the country listView still could be updated?

整个表单提交的是其他的按钮,这样在逻辑上是正常的添加一个国家即使在DateTimeField字段验证错误。

Submit of the whole form is on other button, so logically it is normal to add a country even if there is a validation error in dateTimeField.

谢谢!

P.S。我读过很多关于类似问题的帖子,但大多数被解决了.setReuseItems(真),但它并不在我的情况下工作。

P.S. i've read a lot of posts about similar problem, but most of them were solved with .setReuseItems(true), but it doesn't work in my case.

P.P.S的Apache 1.4.17检票

P.P.S Apache wicket 1.4.17

推荐答案

我在我的项目面临着类似的问题,我找到了解决方法是使用一种特殊的访客。这将更新模型,即使提交的输入无效。

I faced a similar problem in my project, the workaround I found was to use a special Visitor. It will update the model even though the submitted input is invalid.

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(新VisitorUpdateModelWithoutValidation());

Simply use it in the onSubmit method of your behavior : getForm().visitFormComponents(new VisitorUpdateModelWithoutValidation());

这篇关于阿帕奇检票口:如何更新验证错误后,模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆