use of com.vaadin.flow.data.binder.Binder.Binding 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.Binding 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.Binding in project flow by vaadin.
the class BinderTest method save_bound_bean_disable_validation_binding.
@Test
public void save_bound_bean_disable_validation_binding() throws ValidationException {
Binder<Person> binder = new Binder<>();
Binding<Person, String> nameBinding = binder.forField(nameField).withValidator((value, context) -> {
if (value.equals("Mike"))
return ValidationResult.ok();
else
return ValidationResult.error("value must be Mike");
}).bind(Person::getFirstName, Person::setFirstName);
binder.forField(ageField).withConverter(new StringToIntegerConverter("")).bind(Person::getAge, Person::setAge);
Person person = new Person();
String fieldValue = "John";
nameField.setValue(fieldValue);
int age = 10;
ageField.setValue("10");
person.setFirstName("Mark");
nameBinding.setValidatorsDisabled(true);
binder.writeBean(person);
// name is now written as validation was disabled
assertEquals(fieldValue, person.getFirstName());
assertEquals(age, person.getAge());
}
use of com.vaadin.flow.data.binder.Binder.Binding in project flow by vaadin.
the class BinderTest method setRequired_withCustomValidator_fieldGetsRequiredIndicatorAndValidator.
@Test
public void setRequired_withCustomValidator_fieldGetsRequiredIndicatorAndValidator() {
TestTextField textField = new TestTextField();
assertFalse(textField.isRequiredIndicatorVisible());
BindingBuilder<Person, String> binding = binder.forField(textField);
assertFalse(textField.isRequiredIndicatorVisible());
AtomicInteger invokes = new AtomicInteger();
Validator<String> customRequiredValidator = (value, context) -> {
invokes.incrementAndGet();
if (StringUtils.isBlank(value)) {
return ValidationResult.error("Input is required.");
}
return ValidationResult.ok();
};
binding.asRequired(customRequiredValidator);
assertTrue(textField.isRequiredIndicatorVisible());
binding.bind(Person::getFirstName, Person::setFirstName);
binder.setBean(item);
assertThat(textField.getErrorMessage(), isEmptyString());
assertEquals(1, invokes.get());
textField.setValue(" ");
String errorMessage = textField.getErrorMessage();
assertNotNull(errorMessage);
assertEquals("Input is required.", componentErrors.get(textField));
// validation is done for all changed bindings once.
assertEquals(2, invokes.get());
textField.setValue("value");
assertFalse(textField.isInvalid());
assertTrue(textField.isRequiredIndicatorVisible());
}
use of com.vaadin.flow.data.binder.Binder.Binding 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