use of com.vaadin.flow.function.SerializablePredicate in project flow by vaadin.
the class BinderConverterValidatorTest method binderLoad_withCrossFieldValidation_clearsErrors.
@Test
public void binderLoad_withCrossFieldValidation_clearsErrors() {
TestTextField lastNameField = new TestTextField();
final SerializablePredicate<String> lengthPredicate = v -> v.length() > 2;
BindingBuilder<Person, String> firstNameBinding = binder.forField(nameField).withValidator(lengthPredicate, "length");
firstNameBinding.bind(Person::getFirstName, Person::setFirstName);
Binding<Person, String> lastNameBinding = binder.forField(lastNameField).withValidator(v -> !nameField.getValue().isEmpty() || lengthPredicate.test(v), "err").withValidator(lengthPredicate, "length").bind(Person::getLastName, Person::setLastName);
// this will be triggered as a new bean is bound with binder.bind(),
// causing a validation error to be visible until reset is done
nameField.addValueChangeListener(v -> lastNameBinding.validate());
Person person = new Person();
binder.setBean(person);
Assert.assertFalse(componentErrors.containsKey(nameField));
Assert.assertFalse(componentErrors.containsKey(lastNameField));
nameField.setValue("x");
Assert.assertTrue(componentErrors.containsKey(nameField));
Assert.assertTrue(componentErrors.containsKey(lastNameField));
binder.setBean(person);
Assert.assertFalse(componentErrors.containsKey(nameField));
Assert.assertFalse(componentErrors.containsKey(lastNameField));
}
Aggregations