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());
}
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());
}
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());
}
Aggregations