use of com.vaadin.flow.tests.data.bean.Person 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.tests.data.bean.Person in project flow by vaadin.
the class BinderConverterValidatorTest method validatorForSuperTypeCanBeUsed.
@Test
public void validatorForSuperTypeCanBeUsed() {
// Validates that a validator for a super type can be used, e.g.
// validator for Number can be used on a Double
TestTextField salaryField = new TestTextField();
Validator<Number> positiveNumberValidator = (value, context) -> {
if (value.doubleValue() >= 0) {
return ValidationResult.ok();
} else {
return ValidationResult.error(NEGATIVE_ERROR_MESSAGE);
}
};
binder.forField(salaryField).withConverter(Double::valueOf, String::valueOf).withValidator(positiveNumberValidator).bind(Person::getSalaryDouble, Person::setSalaryDouble);
Person person = new Person();
binder.setBean(person);
salaryField.setValue("10");
assertEquals(10, person.getSalaryDouble(), 0);
// Does not pass validator
salaryField.setValue("-1");
assertEquals(10, person.getSalaryDouble(), 0);
assertInvalidField(NEGATIVE_ERROR_MESSAGE, salaryField);
}
use of com.vaadin.flow.tests.data.bean.Person 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.tests.data.bean.Person in project flow by vaadin.
the class BinderConverterValidatorTest method save_beanValidationErrorsWithConverter.
@Test(expected = ValidationException.class)
public void save_beanValidationErrorsWithConverter() throws ValidationException {
Binder<Person> binder = new Binder<>();
binder.forField(ageField).withConverter(new StringToIntegerConverter("Can't convert")).bind(Person::getAge, Person::setAge);
binder.withValidator(Validator.from(person -> false, "b"));
Person person = new Person();
ageField.setValue("1");
try {
binder.writeBean(person);
} finally {
// Bean should have been updated for item validation but reverted
Assert.assertEquals(0, person.getAge());
}
}
use of com.vaadin.flow.tests.data.bean.Person in project flow by vaadin.
the class BinderConverterValidatorTest method binderBindAndLoad_clearsErrors.
@Test
public void binderBindAndLoad_clearsErrors() {
BindingBuilder<Person, String> binding = binder.forField(nameField).withValidator(notEmpty);
binding.bind(Person::getFirstName, Person::setFirstName);
binder.withValidator(bean -> !bean.getFirstName().contains("error"), "error");
Person person = new Person();
person.setFirstName("");
binder.setBean(person);
// initial value is invalid but no error
Assert.assertFalse(componentErrors.containsKey(nameField));
// make error show
nameField.setValue("foo");
nameField.setValue("");
Assert.assertTrue(componentErrors.containsKey(nameField));
// bind to another person to see that error is cleared
person = new Person();
person.setFirstName("");
binder.setBean(person);
// error has been cleared
Assert.assertFalse(componentErrors.containsKey(nameField));
// make show error
nameField.setValue("foo");
nameField.setValue("");
Assert.assertTrue(componentErrors.containsKey(nameField));
// load should also clear error
binder.readBean(person);
Assert.assertFalse(componentErrors.containsKey(nameField));
// bind a new field that has invalid value in bean
TestTextField lastNameField = new TestTextField();
// The test starts with a valid value as the last name of the person,
// since the binder assumes any non-changed values to be valid.
person.setLastName("bar");
BindingBuilder<Person, String> binding2 = binder.forField(lastNameField).withValidator(notEmpty);
binding2.bind(Person::getLastName, Person::setLastName);
// should not have error shown when initialized
assertThat(lastNameField.getErrorMessage(), isEmptyString());
// Set a value that breaks the validation
lastNameField.setValue("");
assertNotNull(lastNameField.getErrorMessage());
// add status label to show bean level error
TestLabel statusLabel = new TestLabel();
binder.setStatusLabel(statusLabel);
nameField.setValue("error");
// no error shown yet because second field validation doesn't pass
Assert.assertEquals("", statusLabel.getText());
// make second field validation pass to get bean validation error
lastNameField.setValue("foo");
Assert.assertEquals("error", statusLabel.getText());
// reload bean to clear error
binder.readBean(person);
Assert.assertEquals("", statusLabel.getText());
// reset() should clear all errors and status label
nameField.setValue("");
lastNameField.setValue("");
Assert.assertTrue(componentErrors.containsKey(nameField));
Assert.assertTrue(componentErrors.containsKey(lastNameField));
binder.removeBean();
Assert.assertFalse(componentErrors.containsKey(nameField));
Assert.assertFalse(componentErrors.containsKey(lastNameField));
Assert.assertEquals("", statusLabel.getText());
}
Aggregations