【问题标题】:How to convert the CheckGroup<> to FormComponent for Validation如何将 CheckGroup<> 转换为 FormComponent 进行验证
【发布时间】:2020-11-27 16:29:16
【问题描述】:

我有一个下拉选项和复选框。已添加代码以在用户 onsubmit 未选择任何这些时引发错误。

CheckGroup billableGroup = new CheckGroup(id, new PropertyModel(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 并将其用于验证。

【问题讨论】:

  • 其实CheckGroup是一个FormCompnent。您可以显示更多来自您的 FormComponentValidator 的代码吗?
  • @Andrea Del Bene - 更新了代码。

标签: java checkbox wicket dropdownchoice


【解决方案1】:

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

【讨论】:

  • 将 FormComponent 更改为通用类型对我有用 :) 非常感谢 @Andrea Del Bene
  • @sharonm 欢迎您。如果您觉得我的回答有用,请不要忘记投票。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-02
  • 1970-01-01
  • 2011-10-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多