Andrew Nepogoda
Andrew Nepogoda

Reputation: 1895

Apache Wicket: textarea lose content after hide/show

I have wicket form with checkbox and textarea. I need to hide and show textarea when checkbox value changed.

This is my implementation:

private class EditCommentForm extends Form {

    private TextArea applyToAllArea;
    private boolean addToAll;

    // some code here

    public EditCommentForm(String id) {
        super(id);
        applyToAllArea = new TextArea<>("applyToAllArea", Model.of(""));
        applyToAllArea.setVisible(addToAll);
        applyToAllArea.setOutputMarkupId(true);
        applyToAllArea.setOutputMarkupPlaceholderTag(true);
        add(applyToAllArea);

        CheckBox addToAllCheckbox = new AjaxCheckBox("addToAll", new PropertyModel<>(this, "addToAll")) {
            @Override
            protected void onUpdate(AjaxRequestTarget target) {
                applyToAllArea.setVisible(addToAll);
                target.addComponent(applyToAllArea);
            }
        };
        addToAllCheckbox.setVisible(documents.size() > 1);
        add(addToAllCheckbox);
        // some code here
    }

    private boolean isAddToAll() {
        return addToAll;
    }

}

When I type some information into textarea and then click on checkbox twice(hide and show textarea) my typed information lose.

So, how I can save typed information without form submit?

Wicket version 1.4.20

Upvotes: 0

Views: 411

Answers (1)

martin-g
martin-g

Reputation: 17533

As @bert explained the problem is that the content of the textarea is not saved anyhow and after repaint Wicket uses the current model at the server side which is empty.

An easy solution is to add new AjaxFormComponentUpdatingBehavior("onblur") to the textarea so that it saves its content when the user moves to another element in the page.

I'd suggest you to upgrade to at least 1.4.22. It has few security related fixes compared to 1.4.20.

Upvotes: 3

Related Questions