V
V
Vanes Ri_Lax2021-07-28 07:35:48
Java
Vanes Ri_Lax, 2021-07-28 07:35:48

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; }
}

Next, I created a textField field and I'm trying to do validation:
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);

And then I had some questions. First of all, what is the purpose of passing the Person class? The line is not entirely clear:
.bind(Person::getSurname, Person::setSurname);
That is, if the validation passes, then the data is written to the corresponding class field?
Sorry for the possibly stupid questions

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
acwartz, 2021-07-28
@vanesxl

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

Y
Yuriy Vorobyov, 2021-07-28
@YuriyVorobyov1333

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?

We could reinvent the wheel, but Binder does everything for us and is the very connecting layer between our class code and user interface forms.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question