From 92eadb98fb0f9b311832180cfda52a49e5279af1 Mon Sep 17 00:00:00 2001 From: filippo-ferrari Date: Sat, 7 Sep 2024 18:38:54 +0200 Subject: [PATCH] feat: validation to expenses and people --- .../munera/views/expenses/ExpensesView.java | 17 +++++++++++++++++ .../munera/views/people/PeopleView.java | 8 +++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/application/munera/views/expenses/ExpensesView.java b/src/main/java/com/application/munera/views/expenses/ExpensesView.java index 69cac84..70da0d6 100644 --- a/src/main/java/com/application/munera/views/expenses/ExpensesView.java +++ b/src/main/java/com/application/munera/views/expenses/ExpensesView.java @@ -24,6 +24,7 @@ import com.vaadin.flow.component.textfield.TextArea; import com.vaadin.flow.component.textfield.TextField; import com.vaadin.flow.data.binder.BeanValidationBinder; import com.vaadin.flow.data.binder.ValidationException; +import com.vaadin.flow.data.converter.StringToBigDecimalConverter; import com.vaadin.flow.data.renderer.ComponentRenderer; import com.vaadin.flow.router.*; import jakarta.annotation.security.PermitAll; @@ -118,6 +119,22 @@ public class ExpensesView extends Div implements BeforeEnterObserver { // Bind fields. This is where you'd define e.g. validation rules binder.bindInstanceFields(this); + binder.forField(name) + .asRequired("Name is required") + .bind(Expense::getName, Expense::setName); + + binder.forField(cost) + .asRequired("Cost is required") + .withConverter( new StringToBigDecimalConverter("Invalid cost")) + .bind(Expense::getCost, Expense::setCost); + + binder.forField(category) + .asRequired("Category is required") + .bind(Expense::getCategory, Expense::setCategory); + + binder.forField(date) + .asRequired("Date is required") + .bind(Expense::getDate, Expense::setDate); // We set initial value of isPeriodic to true and show period fields isPeriodic.setValue(false); diff --git a/src/main/java/com/application/munera/views/people/PeopleView.java b/src/main/java/com/application/munera/views/people/PeopleView.java index 7105ce5..2b177d9 100644 --- a/src/main/java/com/application/munera/views/people/PeopleView.java +++ b/src/main/java/com/application/munera/views/people/PeopleView.java @@ -96,8 +96,14 @@ public class PeopleView extends Div implements BeforeEnterObserver { binder = new BeanValidationBinder<>(Person.class); // Bind fields. This is where you'd define e.g. validation rules - binder.bindInstanceFields(this); + binder.forField(firstName) + .asRequired("First Name is required") + .bind(Person::getFirstName, Person::setFirstName); + + binder.forField(lastName) + .asRequired("Last Name is required") + .bind(Person::getLastName, Person::setLastName); cancel.addClickListener(e -> { clearForm();