1

我有一个 dropdownchoice 和 checkbox 。已添加代码以在用户 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)); 我无法将检查组添加到验证器,因为它没有转换为 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");
            }
        }

请让我知道如何将检查组转换为 FormCompenent 并将其用于验证。

4

1 回答 1

2

CheckGroup 使用集合作为模型对象,而 DropDownChoice 具有单个模型对象。这意味着 CheckGroup< Project> 是无效的,因为它需要一个集合,而 CheckGroup<List< Project>> 是有效的。

您应该“放松” FormComponentValidator 中的类型限制以接受通用 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");
            }


}

注意:对象来自包 wicket.util.lang

于 2020-11-29T15:59:59.470 回答