use of com.vaadin.flow.data.validator.NotEmptyValidator in project flow by vaadin.
the class BinderConverterValidatorTest method saveIfValid_noValidationErrors.
@Test
public void saveIfValid_noValidationErrors() {
String msg = "foo";
binder.forField(nameField).withValidator(new NotEmptyValidator<>(msg)).bind(Person::getFirstName, Person::setFirstName);
Person person = new Person();
person.setFirstName("foo");
nameField.setValue("bar");
Assert.assertTrue(binder.writeBeanIfValid(person));
Assert.assertEquals("bar", person.getFirstName());
}
use of com.vaadin.flow.data.validator.NotEmptyValidator in project flow by vaadin.
the class BinderConverterValidatorTest method save_beanValidationErrors.
@Test(expected = ValidationException.class)
public void save_beanValidationErrors() throws ValidationException {
Binder<Person> binder = new Binder<>();
binder.forField(nameField).withValidator(new NotEmptyValidator<>("a")).bind(Person::getFirstName, Person::setFirstName);
binder.withValidator(Validator.from(person -> false, "b"));
Person person = new Person();
nameField.setValue("foo");
try {
binder.writeBean(person);
} finally {
// Bean should have been updated for item validation but reverted
assertNull(person.getFirstName());
}
}
use of com.vaadin.flow.data.validator.NotEmptyValidator 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.validator.NotEmptyValidator in project flow by vaadin.
the class BinderConverterValidatorTest method save_fieldValidationErrors.
@Test(expected = ValidationException.class)
public void save_fieldValidationErrors() throws ValidationException {
Binder<Person> binder = new Binder<>();
String msg = "foo";
binder.forField(nameField).withValidator(new NotEmptyValidator<>(msg)).bind(Person::getFirstName, Person::setFirstName);
Person person = new Person();
String firstName = "foo";
person.setFirstName(firstName);
nameField.setValue("");
try {
binder.writeBean(person);
} finally {
// Bean should not have been updated
Assert.assertEquals(firstName, person.getFirstName());
}
}
use of com.vaadin.flow.data.validator.NotEmptyValidator 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);
}
Aggregations