Search in sources :

Example 6 with NotEmptyValidator

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

Example 7 with NotEmptyValidator

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

the class BinderConverterValidatorTest method saveIfValid_fieldValidationErrors.

@Test
public void saveIfValid_fieldValidationErrors() {
    String msg = "foo";
    binder.forField(nameField).withValidator(new NotEmptyValidator<>(msg)).bind(Person::getFirstName, Person::setFirstName);
    Person person = new Person();
    person.setFirstName("foo");
    nameField.setValue("");
    Assert.assertFalse(binder.writeBeanIfValid(person));
    Assert.assertEquals("foo", 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 8 with NotEmptyValidator

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

the class BinderConverterValidatorTest method save_fieldsAndBeanLevelValidation.

@Test
public void save_fieldsAndBeanLevelValidation() throws ValidationException {
    binder.forField(nameField).withValidator(new NotEmptyValidator<>("a")).bind(Person::getFirstName, Person::setFirstName);
    binder.withValidator(Validator.from(person -> person.getLastName() != null, "b"));
    Person person = new Person();
    person.setLastName("bar");
    nameField.setValue("foo");
    binder.writeBean(person);
    Assert.assertEquals(nameField.getValue(), person.getFirstName());
    Assert.assertEquals("bar", person.getLastName());
}
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 9 with NotEmptyValidator

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

the class BinderTest method validationStatusHandler_onlyRunForChangedField.

@Test
public void validationStatusHandler_onlyRunForChangedField() {
    TestTextField firstNameField = new TestTextField();
    TestTextField lastNameField = new TestTextField();
    AtomicInteger invokes = new AtomicInteger();
    binder.forField(firstNameField).withValidator(new NotEmptyValidator<>("")).withValidationStatusHandler(validationStatus -> invokes.addAndGet(1)).bind(Person::getFirstName, Person::setFirstName);
    binder.forField(lastNameField).withValidator(new NotEmptyValidator<>("")).bind(Person::getLastName, Person::setLastName);
    binder.setBean(item);
    // setting the bean causes 2:
    Assert.assertEquals(2, invokes.get());
    lastNameField.setValue("");
    Assert.assertEquals(2, invokes.get());
    firstNameField.setValue("");
    Assert.assertEquals(3, invokes.get());
    binder.removeBean();
    Person person = new Person();
    person.setFirstName("a");
    person.setLastName("a");
    binder.readBean(person);
    // reading from a bean causes 2:
    Assert.assertEquals(5, invokes.get());
    lastNameField.setValue("");
    Assert.assertEquals(5, invokes.get());
    firstNameField.setValue("");
    Assert.assertEquals(6, invokes.get());
}
Also used : HasValue(com.vaadin.flow.component.HasValue) Person(com.vaadin.flow.tests.data.bean.Person) HashMap(java.util.HashMap) Assert.assertSame(org.junit.Assert.assertSame) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IntegerRangeValidator(com.vaadin.flow.data.validator.IntegerRangeValidator) Map(java.util.Map) BindingBuilder(com.vaadin.flow.data.binder.Binder.BindingBuilder) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) 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) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) Objects(java.util.Objects) StringLengthValidator(com.vaadin.flow.data.validator.StringLengthValidator) Stream(java.util.stream.Stream) 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) Sex(com.vaadin.flow.tests.data.bean.Sex) Matchers.containsString(org.hamcrest.Matchers.containsString) Assert.assertEquals(org.junit.Assert.assertEquals) NotEmptyValidator(com.vaadin.flow.data.validator.NotEmptyValidator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 10 with NotEmptyValidator

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

the class BinderTest method withValidator_doesNotDisablesDefaulNullRepresentation.

@Test
public void withValidator_doesNotDisablesDefaulNullRepresentation() {
    String nullRepresentation = "foo";
    binder.forField(nameField).withNullRepresentation(nullRepresentation).withValidator(new NotEmptyValidator<>("")).bind(Person::getFirstName, Person::setFirstName);
    item.setFirstName(null);
    binder.setBean(item);
    Assert.assertEquals(nullRepresentation, nameField.getValue());
    String newValue = "bar";
    nameField.setValue(newValue);
    Assert.assertEquals(newValue, item.getFirstName());
}
Also used : NotEmptyValidator(com.vaadin.flow.data.validator.NotEmptyValidator) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Matchers.containsString(org.hamcrest.Matchers.containsString) 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