use of com.vaadin.flow.data.binder.Binder.BindingBuilder in project flow by vaadin.
the class BinderConverterValidatorTest method binderLoad_withCrossFieldValidation_clearsErrors.
@Test
public void binderLoad_withCrossFieldValidation_clearsErrors() {
TestTextField lastNameField = new TestTextField();
final SerializablePredicate<String> lengthPredicate = v -> v.length() > 2;
BindingBuilder<Person, String> firstNameBinding = binder.forField(nameField).withValidator(lengthPredicate, "length");
firstNameBinding.bind(Person::getFirstName, Person::setFirstName);
Binding<Person, String> lastNameBinding = binder.forField(lastNameField).withValidator(v -> !nameField.getValue().isEmpty() || lengthPredicate.test(v), "err").withValidator(lengthPredicate, "length").bind(Person::getLastName, Person::setLastName);
// this will be triggered as a new bean is bound with binder.bind(),
// causing a validation error to be visible until reset is done
nameField.addValueChangeListener(v -> lastNameBinding.validate());
Person person = new Person();
binder.setBean(person);
Assert.assertFalse(componentErrors.containsKey(nameField));
Assert.assertFalse(componentErrors.containsKey(lastNameField));
nameField.setValue("x");
Assert.assertTrue(componentErrors.containsKey(nameField));
Assert.assertTrue(componentErrors.containsKey(lastNameField));
binder.setBean(person);
Assert.assertFalse(componentErrors.containsKey(nameField));
Assert.assertFalse(componentErrors.containsKey(lastNameField));
}
use of com.vaadin.flow.data.binder.Binder.BindingBuilder 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());
}
use of com.vaadin.flow.data.binder.Binder.BindingBuilder 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.binder.Binder.BindingBuilder 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);
}
use of com.vaadin.flow.data.binder.Binder.BindingBuilder 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);
}
Aggregations