use of com.vaadin.flow.data.validator.NotEmptyValidator 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);
}
use of com.vaadin.flow.data.validator.NotEmptyValidator in project flow by vaadin.
the class BinderConverterValidatorTest method saveIfValid_fieldValidationErrors.
@Test
public void saveIfValid_fieldValidationErrors() {
String msg = "foo";
binder.forField(nameField).withValidator(new NotEmptyValidator<>(msg)).bind(Person::getFirstName, Person::setFirstName);
Person person = new Person();
person.setFirstName("foo");
nameField.setValue("");
Assert.assertFalse(binder.writeBeanIfValid(person));
Assert.assertEquals("foo", person.getFirstName());
}
use of com.vaadin.flow.data.validator.NotEmptyValidator in project flow by vaadin.
the class BinderConverterValidatorTest method save_fieldsAndBeanLevelValidation.
@Test
public void save_fieldsAndBeanLevelValidation() throws ValidationException {
binder.forField(nameField).withValidator(new NotEmptyValidator<>("a")).bind(Person::getFirstName, Person::setFirstName);
binder.withValidator(Validator.from(person -> person.getLastName() != null, "b"));
Person person = new Person();
person.setLastName("bar");
nameField.setValue("foo");
binder.writeBean(person);
Assert.assertEquals(nameField.getValue(), person.getFirstName());
Assert.assertEquals("bar", person.getLastName());
}
use of com.vaadin.flow.data.validator.NotEmptyValidator in project flow by vaadin.
the class BinderTest method validationStatusHandler_onlyRunForChangedField.
@Test
public void validationStatusHandler_onlyRunForChangedField() {
TestTextField firstNameField = new TestTextField();
TestTextField lastNameField = new TestTextField();
AtomicInteger invokes = new AtomicInteger();
binder.forField(firstNameField).withValidator(new NotEmptyValidator<>("")).withValidationStatusHandler(validationStatus -> invokes.addAndGet(1)).bind(Person::getFirstName, Person::setFirstName);
binder.forField(lastNameField).withValidator(new NotEmptyValidator<>("")).bind(Person::getLastName, Person::setLastName);
binder.setBean(item);
// setting the bean causes 2:
Assert.assertEquals(2, invokes.get());
lastNameField.setValue("");
Assert.assertEquals(2, invokes.get());
firstNameField.setValue("");
Assert.assertEquals(3, invokes.get());
binder.removeBean();
Person person = new Person();
person.setFirstName("a");
person.setLastName("a");
binder.readBean(person);
// reading from a bean causes 2:
Assert.assertEquals(5, invokes.get());
lastNameField.setValue("");
Assert.assertEquals(5, invokes.get());
firstNameField.setValue("");
Assert.assertEquals(6, invokes.get());
}
use of com.vaadin.flow.data.validator.NotEmptyValidator in project flow by vaadin.
the class BinderTest method withValidator_doesNotDisablesDefaulNullRepresentation.
@Test
public void withValidator_doesNotDisablesDefaulNullRepresentation() {
String nullRepresentation = "foo";
binder.forField(nameField).withNullRepresentation(nullRepresentation).withValidator(new NotEmptyValidator<>("")).bind(Person::getFirstName, Person::setFirstName);
item.setFirstName(null);
binder.setBean(item);
Assert.assertEquals(nullRepresentation, nameField.getValue());
String newValue = "bar";
nameField.setValue(newValue);
Assert.assertEquals(newValue, item.getFirstName());
}
Aggregations