feat: validation to expenses and people

This commit is contained in:
filippo-ferrari 2024-09-07 18:38:54 +02:00
parent a05c77e8ff
commit 92eadb98fb
2 changed files with 24 additions and 1 deletions

View file

@ -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);

View file

@ -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();