How to convert the CheckGroup<> to FormComponent for Validation

110 views Asked by At

I have a dropdownchoice and checkbox . Have added code to throw error when none of these are selected by the user onsubmit .

CheckGroup billableGroup = new CheckGroup<>(id, new PropertyModel<Collection>(billableProjects, "projects")); billableGroup.add(new CheckGroupSelector("checkall"));

DropDownChoice billableProjectsList = new DropDownChoice<>( ......... new ChoiceRenderer("fullNameWithCustomer")); billableProjectsList.setLabel(new ResourceModel("printMonth.billable"));

form.add(new FormComponentValidator(billableProjectsList, billableGroup)); I am unable to add the checkgroup to the Validator since its not converting to FormCompnent.

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

    @SuppressWarnings("unchecked")
    public FormComponentValidator(FormComponent<Project> selectedBillableProject, FormComponent<Project> 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())) {

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

Please let me know how to convert the checkgroup to FormCompenent and use it for validation.

1

There are 1 answers

2
Andrea Del Bene On BEST ANSWER

CheckGroup uses a collection as model object while DropDownChoice has a single model object. This means that CheckGroup< Project> is not valid as it requires a collections, while CheckGroup<List< Project>> is valid.

You should "relax" the type restriction in your FormComponentValidator to accept generic FormComponentS:

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 ("".equals(Objects.stringValue(components[0].getInput(), true))
            && "".equals(Objects.stringValue(components[1].getInput(), true))) {

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


}

NOTE: Objects is from package wicket.util.lang