Comic Sans MS Lover
Comic Sans MS Lover

Reputation: 1729

Wicket - IValidator not calling validate when TextField is empty

I have a class that implements the IValidator. I add this validator class to my TextField, and the overrided method validate(Invalidatable<T>) is called. However, if the TextFieldis empty, the method is not called, and the validation does not occurs. Why is this happening? Is this expected behavior?

Validator Class

public class CorporateNameValidator implements IValidator<String> {

        private static final String ERROR_EMPTY = "Error";

        @Override
        public void validate(IValidatable<String> validatable) {

            //Method not called when TextField has blank value.

            final String name = validatable.getValue();

            info("NAME: " + name);
        }
    }

Instantiating TextField

corporateNameInput = new TextField<String>(CORPORATE_NAME_INPUT_ID, new PropertyModel<String>(this, ""));

Setting TextField Properties

corporateNameInput.add(new CorporateNameValidator()); corporateNameInput.setOutputMarkupPlaceholderTag(true);

And then I add the TextField to the Form.

Upvotes: 2

Views: 2862

Answers (1)

Martijn Dashorst
Martijn Dashorst

Reputation: 3682

From the JavaDoc of IValidator:

Interface representing a validator that can validate an IValidatable object.

Unless the validator implements the INullAcceptingValidator interface as well, Wicket will not pass null values to the IValidator#validate(IValidatable) method.

It is as designed, and you should implement INullAcceptingValidator instead.

Upvotes: 11

Related Questions