How to add Validators in Vaadin 8?

Diego D picture Diego D · Feb 27, 2017 · Viewed 11.5k times · Source

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

Diego D picture Diego D · Feb 27, 2017

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