Search in sources :

Example 1 with StringLengthValidator

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

the class BinderInstanceFieldTest method bindInstanceFields_bindNotBoundFieldsOnly_customBindingIsNotReplaced.

@Test
public void bindInstanceFields_bindNotBoundFieldsOnly_customBindingIsNotReplaced() {
    BindAllFields form = new BindAllFields();
    Binder<Person> binder = new Binder<>(Person.class);
    TestTextField name = new TestTextField();
    form.firstName = name;
    binder.forField(form.firstName).withValidator(new StringLengthValidator("Name is invalid", 3, 10)).bind("firstName");
    binder.bindInstanceFields(form);
    Person person = new Person();
    String personName = "foo";
    person.setFirstName(personName);
    person.setBirthDate(LocalDate.now());
    binder.setBean(person);
    Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
    Assert.assertEquals(person.getBirthDate(), form.birthDate.getValue());
    // the instance is not overridden
    Assert.assertEquals(name, form.firstName);
    // Test automatic binding
    form.birthDate.setValue(person.getBirthDate().plusDays(345));
    Assert.assertEquals(form.birthDate.getValue(), person.getBirthDate());
    // Test custom binding
    form.firstName.setValue("aa");
    Assert.assertEquals(personName, person.getFirstName());
    Assert.assertFalse(binder.validate().isOk());
}
Also used : StringLengthValidator(com.vaadin.flow.data.validator.StringLengthValidator) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Example 2 with StringLengthValidator

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

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

the class BinderInstanceFieldTest method bindInstanceFields_fieldsAreConfigured_customBindingIsNotReplaced.

@Test
public void bindInstanceFields_fieldsAreConfigured_customBindingIsNotReplaced() {
    BindWithNoFieldInPerson form = new BindWithNoFieldInPerson();
    Binder<Person> binder = new Binder<>(Person.class);
    TestTextField name = new TestTextField();
    form.firstName = name;
    binder.forField(form.firstName).withValidator(new StringLengthValidator("Name is invalid", 3, 10)).bind("firstName");
    TestTextField ageField = new TestTextField();
    form.noFieldInPerson = ageField;
    binder.forField(form.noFieldInPerson).withConverter(new StringToIntegerConverter("")).bind(Person::getAge, Person::setAge);
    binder.bindInstanceFields(form);
    Person person = new Person();
    String personName = "foo";
    int age = 11;
    person.setFirstName(personName);
    person.setAge(age);
    binder.setBean(person);
    Assert.assertEquals(person.getFirstName(), form.firstName.getValue());
    Assert.assertEquals(String.valueOf(person.getAge()), form.noFieldInPerson.getValue());
    // the instances are not overridden
    Assert.assertEquals(name, form.firstName);
    Assert.assertEquals(ageField, form.noFieldInPerson);
    // Test correct age
    age += 56;
    form.noFieldInPerson.setValue(String.valueOf(age));
    Assert.assertEquals(form.noFieldInPerson.getValue(), String.valueOf(person.getAge()));
    // Test incorrect name
    form.firstName.setValue("aa");
    Assert.assertEquals(personName, person.getFirstName());
    Assert.assertFalse(binder.validate().isOk());
}
Also used : StringLengthValidator(com.vaadin.flow.data.validator.StringLengthValidator) TestTextField(com.vaadin.flow.data.binder.testcomponents.TestTextField) StringToIntegerConverter(com.vaadin.flow.data.converter.StringToIntegerConverter) Person(com.vaadin.flow.tests.data.bean.Person) Test(org.junit.Test)

Aggregations

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