Search in sources :

Example 1 with BindingBuilder

use of com.vaadin.flow.data.binder.Binder.BindingBuilder 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)

Example 2 with BindingBuilder

use of com.vaadin.flow.data.binder.Binder.BindingBuilder in project flow by vaadin.

the class BinderConverterValidatorTest method validate_failedBothBeanValidatorAndFieldValidator.

@Test
public void validate_failedBothBeanValidatorAndFieldValidator() {
    String msg1 = "foo";
    BindingBuilder<Person, String> binding = binder.forField(nameField).withValidator(new NotEmptyValidator<>(msg1));
    binding.bind(Person::getFirstName, Person::setFirstName);
    String msg2 = "bar";
    binder.withValidator(Validator.from(bean -> false, msg2));
    Person person = new Person();
    binder.setBean(person);
    List<BindingValidationStatus<?>> errors = binder.validate().getFieldValidationErrors();
    assertEquals(1, errors.size());
    BindingValidationStatus<?> error = errors.get(0);
    assertEquals(msg1, error.getMessage().get());
    assertEquals(nameField, error.getField());
    assertEquals(msg1, nameField.getErrorMessage());
}
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) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 3 with BindingBuilder

use of com.vaadin.flow.data.binder.Binder.BindingBuilder in project flow by vaadin.

the class BinderTest method multiple_calls_to_same_binding_builder.

@Test
public void multiple_calls_to_same_binding_builder() {
    String stringLength = "String length failure";
    String conversion = "Conversion failed";
    String ageLimit = "Age not in valid range";
    BindingValidationStatus validation;
    binder = new Binder<>(Person.class);
    BindingBuilder builder = binder.forField(ageField);
    builder.withValidator(new StringLengthValidator(stringLength, 0, 3));
    builder.withConverter(new StringToIntegerConverter(conversion));
    builder.withValidator(new IntegerRangeValidator(ageLimit, 3, 150));
    Binding<Person, ?> bind = builder.bind("age");
    binder.setBean(item);
    ageField.setValue("123123");
    validation = bind.validate();
    Assert.assertTrue(validation.isError());
    Assert.assertEquals(stringLength, validation.getMessage().get());
    ageField.setValue("age");
    validation = bind.validate();
    Assert.assertTrue(validation.isError());
    Assert.assertEquals(conversion, validation.getMessage().get());
    ageField.setValue("256");
    validation = bind.validate();
    Assert.assertTrue(validation.isError());
    Assert.assertEquals(ageLimit, validation.getMessage().get());
    ageField.setValue("30");
    validation = bind.validate();
    Assert.assertFalse(validation.isError());
    Assert.assertEquals(30, item.getAge());
}
Also used : IntegerRangeValidator(com.vaadin.flow.data.validator.IntegerRangeValidator) BindingBuilder(com.vaadin.flow.data.binder.Binder.BindingBuilder) StringLengthValidator(com.vaadin.flow.data.validator.StringLengthValidator) StringToIntegerConverter(com.vaadin.flow.data.converter.StringToIntegerConverter) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Matchers.containsString(org.hamcrest.Matchers.containsString) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 4 with BindingBuilder

use of com.vaadin.flow.data.binder.Binder.BindingBuilder 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) 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) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 5 with BindingBuilder

use of com.vaadin.flow.data.binder.Binder.BindingBuilder in project flow by vaadin.

the class BinderConverterValidatorTest method validate_failedBeanValidatorWithFieldValidator.

@Test
public void validate_failedBeanValidatorWithFieldValidator() {
    String msg = "foo";
    BindingBuilder<Person, String> binding = binder.forField(nameField).withValidator(new NotEmptyValidator<>(msg));
    binding.bind(Person::getFirstName, Person::setFirstName);
    binder.withValidator(Validator.from(bean -> false, msg));
    Person person = new Person();
    binder.setBean(person);
    List<BindingValidationStatus<?>> errors = binder.validate().getFieldValidationErrors();
    assertEquals(1, errors.size());
    BindingValidationStatus<?> error = errors.get(0);
    assertEquals(msg, error.getMessage().get());
    assertEquals(nameField, error.getField());
    assertInvalidField(msg, nameField);
}
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) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Aggregations

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