use of com.vaadin.flow.data.binder.Binder.Binding in project flow by vaadin.
the class BinderConverterValidatorTest method binder_saveIfValid.
@Test
public void binder_saveIfValid() {
String msg1 = "foo";
BindingBuilder<Person, String> binding = binder.forField(nameField).withValidator(new NotEmptyValidator<>(msg1));
binding.bind(Person::getFirstName, Person::setFirstName);
String beanValidatorErrorMessage = "bar";
binder.withValidator(Validator.from(bean -> false, beanValidatorErrorMessage));
Person person = new Person();
String firstName = "first name";
person.setFirstName(firstName);
binder.readBean(person);
nameField.setValue("");
assertFalse(binder.writeBeanIfValid(person));
// check that field level-validation failed and bean is not updated
assertEquals(firstName, person.getFirstName());
assertInvalidField(msg1, nameField);
nameField.setValue("new name");
assertFalse(binder.writeBeanIfValid(person));
// Bean is updated but reverted
assertEquals(firstName, person.getFirstName());
assertValidField(nameField);
}
Aggregations