Change form biding to use BeanValidationBinder

This commit is contained in:
Marcus Hellberg 2024-04-02 10:02:00 -07:00
parent 9d258aa505
commit d46ff97367
2 changed files with 5 additions and 4 deletions

View File

@ -9,6 +9,7 @@ import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout; import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout; import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField; import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.binder.BeanValidationBinder;
import com.vaadin.flow.data.binder.Binder; import com.vaadin.flow.data.binder.Binder;
public class EmployeeEditor extends Composite<VerticalLayout> { public class EmployeeEditor extends Composite<VerticalLayout> {
@ -31,7 +32,7 @@ public class EmployeeEditor extends Composite<VerticalLayout> {
private DeleteListener deleteListener; private DeleteListener deleteListener;
private CancelListener cancelListener; private CancelListener cancelListener;
private final Binder<Employee> binder = new Binder<>(Employee.class); private final Binder<Employee> binder = new BeanValidationBinder<>(Employee.class);
public EmployeeEditor() { public EmployeeEditor() {
var firstName = new TextField("First name"); var firstName = new TextField("First name");
@ -41,8 +42,8 @@ public EmployeeEditor() {
var cancel = new Button("Cancel"); var cancel = new Button("Cancel");
var delete = new Button("Delete", VaadinIcon.TRASH.create()); var delete = new Button("Delete", VaadinIcon.TRASH.create());
binder.forField(firstName).bind(Employee::getFirstName, Employee::setFirstName); binder.forField(firstName).bind("firstName");
binder.forField(lastName).bind(Employee::getLastName, Employee::setLastName); binder.forField(lastName).bind("lastName");
save.addThemeVariants(ButtonVariant.LUMO_PRIMARY); save.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
save.addClickListener(e -> save()); save.addClickListener(e -> save());

View File

@ -37,7 +37,7 @@ public class EmployeesView extends VerticalLayout {
addButton.addClickListener(e -> editEmployee(new Employee())); addButton.addClickListener(e -> editEmployee(new Employee()));
filter.setPlaceholder("Filter by last name"); filter.setPlaceholder("Filter by last name");
filter.setValueChangeMode(ValueChangeMode.EAGER); filter.setValueChangeMode(ValueChangeMode.LAZY);
filter.addValueChangeListener(e -> updateEmployees(e.getValue())); filter.addValueChangeListener(e -> updateEmployees(e.getValue()));
grid.setHeight("200px"); grid.setHeight("200px");