如何将 CheckGroup<> 转换为 FormComponent 以进行验证

How to convert the CheckGroup<> to FormComponent for Validation

我有一个下拉选项和复选框。添加了代码以在用户提交时选择 none 时抛出错误。

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

DropDownChoice billableProjectsList = new DropDownChoice<>( ………… 新的 ChoiceRenderer("fullNameWithCustomer")); billableProjectsList.setLabel(新资源模型("printMonth.billable"));

form.add(新的 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 并将其用于验证。

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

您应该“放宽” 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