Search in sources :

Example 1 with SerializablePredicate

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));
}
Also used : HasValue(com.vaadin.flow.component.HasValue) Person(com.vaadin.flow.tests.data.bean.Person) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Label(com.vaadin.flow.component.html.Label) HashMap(java.util.HashMap) Assert.assertThat(org.junit.Assert.assertThat) HasValidation(com.vaadin.flow.component.HasValidation) Map(java.util.Map) Matchers.hasSize(org.hamcrest.Matchers.hasSize) BindingBuilder(com.vaadin.flow.data.binder.Binder.BindingBuilder) SerializablePredicate(com.vaadin.flow.function.SerializablePredicate) Before(org.junit.Before) StringToIntegerConverter(com.vaadin.flow.data.converter.StringToIntegerConverter) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Test(org.junit.Test) Serializable(java.io.Serializable) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) NotEmptyValidator(com.vaadin.flow.data.validator.NotEmptyValidator) Assert(org.junit.Assert) Binding(com.vaadin.flow.data.binder.Binder.Binding) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Assert.assertEquals(org.junit.Assert.assertEquals) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Aggregations

HasValidation (com.vaadin.flow.component.HasValidation)1 HasValue (com.vaadin.flow.component.HasValue)1 Label (com.vaadin.flow.component.html.Label)1 Binding (com.vaadin.flow.data.binder.Binder.Binding)1 BindingBuilder (com.vaadin.flow.data.binder.Binder.BindingBuilder)1 TestTextField (com.vaadin.flow.data.binder.testcomponents.TestTextField)1 StringToIntegerConverter (com.vaadin.flow.data.converter.StringToIntegerConverter)1 NotEmptyValidator (com.vaadin.flow.data.validator.NotEmptyValidator)1 SerializablePredicate (com.vaadin.flow.function.SerializablePredicate)1 Person (com.vaadin.flow.tests.data.bean.Person)1 Serializable (java.io.Serializable)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Matchers.hasSize (org.hamcrest.Matchers.hasSize)1 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)1 Assert (org.junit.Assert)1 Assert.assertEquals (org.junit.Assert.assertEquals)1 Assert.assertFalse (org.junit.Assert.assertFalse)1