Search in sources :

Example 6 with Binding

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);
}
Also used : HasValue(com.vaadin.flow.component.HasValue) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) Person(com.vaadin.flow.tests.data.bean.Person) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Assert.assertThat(org.junit.Assert.assertThat) HasValidation(com.vaadin.flow.component.HasValidation) Map(java.util.Map) BindingBuilder(com.vaadin.flow.data.binder.Binder.BindingBuilder) Matchers.hasSize(org.hamcrest.Matchers.hasSize) 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) Test(org.junit.Test) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) 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) TestLabel(com.vaadin.flow.data.binder.testcomponents.TestLabel) 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) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Aggregations

HasValue (com.vaadin.flow.component.HasValue)6 Binding (com.vaadin.flow.data.binder.Binder.Binding)6 BindingBuilder (com.vaadin.flow.data.binder.Binder.BindingBuilder)6 TestTextField (com.vaadin.flow.data.binder.testcomponents.TestTextField)6 StringToIntegerConverter (com.vaadin.flow.data.converter.StringToIntegerConverter)6 NotEmptyValidator (com.vaadin.flow.data.validator.NotEmptyValidator)6 CurrentInstance (com.vaadin.flow.internal.CurrentInstance)6 Person (com.vaadin.flow.tests.data.bean.Person)6 Serializable (java.io.Serializable)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)6 Assert (org.junit.Assert)6 Assert.assertEquals (org.junit.Assert.assertEquals)6 Assert.assertFalse (org.junit.Assert.assertFalse)6 Assert.assertNotNull (org.junit.Assert.assertNotNull)6 Assert.assertNull (org.junit.Assert.assertNull)6 Assert.assertTrue (org.junit.Assert.assertTrue)6 Before (org.junit.Before)6