Apache wicket: how to update model after validatio

2019-05-29 03:55发布

问题:

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

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

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?

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.

Thanks!

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 wicket 1.4.17

回答1:

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

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



回答2:

As an update to this answer, in Wicket 6, you can accomplish this by overriding onError() in the Form:

@Override
    protected void onError() {
    super.onError();
    this.updateFormComponentModels();
}


回答3:

You can issue a field.clearInput() on the fields you are updating before you target the update(s).