mardi 24 novembre 2020

How to add validation for Dropdownchoice and Checkbox in Wicket java

I have a checkbox and dropdown in a html page . On click of submit if none of them are selected , their should be a error message that any one of them should be mandatory field and asked to select one value atleast.

I created a new class as as FormValidator implementing IValidator and override the validate method. But i am unaware how to use the dropdown choice value and checkbox value on submit method call ad how to display in html .

Java code -

IModel<Project> dropdownModel =   new PropertyModel<Project>(criteria,"selectedBillableProject");
        DropDownChoice<Project> billableProjectsList = new DropDownChoice<>(
                "projectsList",
                dropdownModel,
                billableProjects,
                new ChoiceRenderer<Project>("fullNameWithCustomer"));
        billableProjectsList.setLabel(new ResourceModel("printMonth.billable"));
        billableProjectsList.setRequired(true);
form.add(new FormComponentValidator(billableProjectsList.getModelObject()));
        
        form.add(billableProjectsList);


public class FormComponentValidator  implements IFormValidator {
    String projectName;
    public FormComponentValidator(Project modelObject){

        projectName  = modelObject.getFullNameWithCustomer();
    }

    @Override
    public FormComponent<?>[] getDependentFormComponents() {
        return new FormComponent[0];
    }

    @Override
    public void validate(Form<?> form) {

        if(projectName.equals(null)){
            form.error("project.required");
        }

    }
}



Aucun commentaire:

Enregistrer un commentaire