sharonm
sharonm

Reputation: 127

Error message displayed by FeedbackPanel does not clear on valid input submission in Wicket

I have a form where there is a dropdown and checkbox. When I don't provide any input, I get the error message set through the Feedback Panel as below code

private Form<ReportCriteria> createCriteriaPanel(String id, IModel<ReportCriteria> model) {
            SelectionForm form = new SelectionForm(id, model);
            ReportCriteria criteria = (ReportCriteria) getDefaultModelObject();
           .......
           FeedbackPanel fb = new FeedbackPanel("feedback");
        fb.setOutputMarkupId(true);
        form.add(fb);
       DropDownChoice<Project> billableProjectsList = new DropDownChoice<>(
                    "projectsList",
                    ......
                    new ChoiceRenderer<Project>("fullNameWithCustomer"));
           
            form.add(billableProjectsList);
            
            CheckGroup<Project> unbillablePrjct = .......
    
            form.add(new FormComponentValidator(billableProjectsList, unbillablePrjct));
            form.getFeedbackMessages().clear();
    }
    
     protected void onSubmit() {
                
               SelectionForm.this.getFeedbackMessages().size());
              TimesheetExportCriteriaPanel.this.SelectionForm.findForm("criteriaForm").getFeedbackMessages();
}

All the feedbackmessages value is empty and not retrieving anything. I receive the error message from FormValidator correctly when I don't provide any inputs. When I provide the inputs and submit, the form refreshes and loads with the same error message.

I want it to clear when we provide valid input.

When checked on forums, I found for Wicket 6.x we need to use component.getFeedbackMessages(), but it is empty for me .

I am new to wicket. Please help with the inputs.

public class FormComponentValidator extends AbstractFormValidator {
    private static final long serialVersionUID = 1L;
    private FormComponent<?>[] components;

    @SuppressWarnings("unchecked")
    public FormComponentValidator(FormComponent<?> selectedBillableProject, FormComponent<?> selectedUnBillableProject) {
        components = new FormComponent[]{selectedBillableProject, selectedUnBillableProject};
    }

    /*
     * (non-Javadoc)
     * @see org.apache.wicket.markup.html.form.validation.IFormValidator#getDependentFormComponents()
     */
    public FormComponent<?>[] getDependentFormComponents() {
        return components;
    }

    /*
     * (non-Javadoc)
     * @see org.apache.wicket.markup.html.form.validation.IFormValidator#validate(org.apache.wicket.markup.html.form.Form)
     */
    public void validate(Form<?> form ) {

        if ((org.apache.commons.lang.StringUtils.isEmpty(components[0].getInput()) || components[0].getInput() == null )
                && (org.apache.commons.lang.StringUtils.isEmpty(components[1].getInput()) || components[1].getInput() == null)) {

                error(components[0], "project.Required");

            }
        }
    }

private void configureFeedback() {

    // activate feedback panel
    final FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
    feedbackPanel.setOutputMarkupId(true);
    feedbackPanel.setVisible(false);

    add(feedbackPanel);

    // don't show filtered feedback errors in feedback panel
    final int[] filteredErrorLevels = new int[] { FeedbackMessage.ERROR };
    feedbackPanel.setFilter(new IFeedbackMessageFilter() {

        @Override
        public boolean accept(FeedbackMessage message) {

            for (int errorLevel : filteredErrorLevels) {
                if (message.getLevel() == errorLevel) {
                    return false;
                }
            }

            return true;
        }

Upvotes: 0

Views: 883

Answers (1)

martin-g
martin-g

Reputation: 17503

When submitting a Form Wicket runs all configured validators on all FormComponents (like TextInput, TextArea, DropDownChoice, etc.). If there is a validation error then a FeedbackMessage is associated with its respective FormComponent.

When rendering a FeedbackPanel Wicket visits all FormComponents and renders their FeedbackMessages (errors, infos, etc.).

At the end of the request cycle Wicket removes all rendered FeedbackMessages. If a FeedbackMessage is not rendered by any IFeedback component (like FeedbackPanel) then it will stay for the next request cycle. This is done by DefaultCleanupFeedbackMessageFilter.

Put a breakpoint at DefaultCleanupFeedbackMessageFilter#accept() and see whether the problematic message is being accepted or not.

Upvotes: 3

Related Questions