Search in sources :

Example 1 with NotEmptyValidator

use of com.vaadin.flow.data.validator.NotEmptyValidator in project flow by vaadin.

the class BinderConverterValidatorTest method saveIfValid_noValidationErrors.

@Test
public void saveIfValid_noValidationErrors() {
    String msg = "foo";
    binder.forField(nameField).withValidator(new NotEmptyValidator<>(msg)).bind(Person::getFirstName, Person::setFirstName);
    Person person = new Person();
    person.setFirstName("foo");
    nameField.setValue("bar");
    Assert.assertTrue(binder.writeBeanIfValid(person));
    Assert.assertEquals("bar", person.getFirstName());
}
Also used : NotEmptyValidator(com.vaadin.flow.data.validator.NotEmptyValidator) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 2 with NotEmptyValidator

use of com.vaadin.flow.data.validator.NotEmptyValidator in project flow by vaadin.

the class BinderConverterValidatorTest method save_beanValidationErrors.

@Test(expected = ValidationException.class)
public void save_beanValidationErrors() throws ValidationException {
    Binder<Person> binder = new Binder<>();
    binder.forField(nameField).withValidator(new NotEmptyValidator<>("a")).bind(Person::getFirstName, Person::setFirstName);
    binder.withValidator(Validator.from(person -> false, "b"));
    Person person = new Person();
    nameField.setValue("foo");
    try {
        binder.writeBean(person);
    } finally {
        // Bean should have been updated for item validation but reverted
        assertNull(person.getFirstName());
    }
}
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) NotEmptyValidator(com.vaadin.flow.data.validator.NotEmptyValidator) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 3 with NotEmptyValidator

use of com.vaadin.flow.data.validator.NotEmptyValidator 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 4 with NotEmptyValidator

use of com.vaadin.flow.data.validator.NotEmptyValidator in project flow by vaadin.

the class BinderConverterValidatorTest method save_fieldValidationErrors.

@Test(expected = ValidationException.class)
public void save_fieldValidationErrors() throws ValidationException {
    Binder<Person> binder = new Binder<>();
    String msg = "foo";
    binder.forField(nameField).withValidator(new NotEmptyValidator<>(msg)).bind(Person::getFirstName, Person::setFirstName);
    Person person = new Person();
    String firstName = "foo";
    person.setFirstName(firstName);
    nameField.setValue("");
    try {
        binder.writeBean(person);
    } finally {
        // Bean should not have been updated
        Assert.assertEquals(firstName, person.getFirstName());
    }
}
Also used : NotEmptyValidator(com.vaadin.flow.data.validator.NotEmptyValidator) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 5 with NotEmptyValidator

use of com.vaadin.flow.data.validator.NotEmptyValidator 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)

Aggregations

NotEmptyValidator (com.vaadin.flow.data.validator.NotEmptyValidator)10 Person (com.vaadin.flow.tests.data.bean.Person)10 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)10 Test (org.junit.Test)10 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 HashMap (java.util.HashMap)6 Map (java.util.Map)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 HasValidation (com.vaadin.flow.component.HasValidation)5 Label (com.vaadin.flow.component.html.Label)5