Search in sources :

Example 1 with IntegerRangeValidator

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

Aggregations

BindingBuilder (com.vaadin.flow.data.binder.Binder.BindingBuilder)1 StringToIntegerConverter (com.vaadin.flow.data.converter.StringToIntegerConverter)1 IntegerRangeValidator (com.vaadin.flow.data.validator.IntegerRangeValidator)1 StringLengthValidator (com.vaadin.flow.data.validator.StringLengthValidator)1 Person (com.vaadin.flow.tests.data.bean.Person)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)1 Test (org.junit.Test)1