我有一个 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 并将其用于验证。