如何将checkgroup< >转换为formcomponent进行验证

dphi5xsq  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(349)

我有一个下拉选择和复选框。添加了一些代码,当用户在提交时没有选择任何一个时抛出错误。
checkgroup billablegroup=new checkgroup<>(id,new propertymodel(billableprojects,“projects”);billablegroup.add(新的checkgroupselector(“checkall”));
dropdownchoice billableprojectslist=新建dropdownchoice<>(。。。。。。。。。新的ChoiceRender(“FullName WithCustomer”);billableprojectslist.setlabel(新资源模型(“printmonth.billable”));
add(新的formcomponentvalidator(billableprojectslist,billablegroup));我无法将checkgroup添加到验证程序,因为它没有转换为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");
            }
        }

请让我知道如何将checkgroup转换为formcompent并使用它进行验证。

k3fezbri

k3fezbri1#

checkgroup使用集合作为模型对象,而dropdownchoice只有一个模型对象。这意味着checkgroup无效,因为它需要集合,而checkgroup<list>有效。
您应该“放宽”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包

相关问题