Diego D Diego D -4 years ago 488
Java Question

How to add Validators in Vaadin 8?

In Vaadin 7 there was an addValidator function, but in Vaadin 8 it does not exist.

Vaadin 7 Example:

TextField user = new TextField("User:");
user.setRequired(true);
user.setInputPrompt("Your username");
user.addValidator(new NullValidator("Username can't be empty", false));
user.setInvalidAllowed(false);

Answer Source

I found the answer here: Whats New

Example:

new Binder<Person>().forField(tf)
    .withValidator(str -> str.length() == 4, "Must be 4 chars")
    .withConverter(new StringToIntegerConverter("Must be Integer"))
    .withValidator(integer -> integer.equals(2017), "Wrong date")
    .bind(Person::getBirthYear, Person::setBirthYear);

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download