Answer the question
In order to leave comments, you need to log in
How does validation work in vaadin 8?
Good afternoon! I study vaadin 8, it is necessary for work.
I threw in the form, created the Person class:
package org.example;
import java.util.Date;
public class Person {
private String surname;
private String name;
private String middleName;
private Date birthday;
private String snils;
public void setSurname(String surname){
this.surname = surname;
}
public String getSurname(){
return surname;
}
public void setName(String name) { this.name = name; }
public String getName() { return name; }
public void setMiddleName(String middleName) { this.middleName = middleName; }
public String getMiddleName() { return middleName; }
public void setBirthday(Date birthday) { this.birthday = birthday; }
public Date getBirthday() { return birthday; }
public void setSnils(String snils) { this.snils = snils; }
public String getSnils() { return snils; }
}
final TextField surname = new TextField("Фамилия");
surname.setWidth("50%");
surname.setMaxLength(100);
new Binder<Person>().forField(surname)
.asRequired("Поле обязательно для заполнения")
.withValidator(str -> str.length() >= 4, "Фамилия не должна быть короче 4 символов")
.bind(Person::getSurname, Person::setSurname);
.bind(Person::getSurname, Person::setSurname);
Answer the question
In order to leave comments, you need to log in
The Binder class binds the UI element to the Person class, and itself manages the transfer of data to the getter / setter when the field is considered valid, for this, references to the getter / setter methods of the Person class are passed to .bind
In essence, Binder is a function of binding to forms, i.e. we bind the object to the fields to display. Binder, in addition to validation, will also convert field values to class field values. And the .bind() function is usually the end of the binding process, this is where we do the binding to our main class for verification.
Basically, you can say this:
That is, if the validation passes, then the data is written to the corresponding class field?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question