use of com.vaadin.flow.data.converter.Converter in project flow by vaadin.
the class BinderTest method setRequired_withCustomValidator_modelConverterBeforeValidator.
@Test
public void setRequired_withCustomValidator_modelConverterBeforeValidator() {
TestTextField textField = new TestTextField();
assertFalse(textField.isRequiredIndicatorVisible());
Converter<String, String> stringBasicPreProcessingConverter = new Converter<String, String>() {
@Override
public Result<String> convertToModel(String value, ValueContext context) {
if (StringUtils.isBlank(value)) {
return Result.ok(null);
}
return Result.ok(StringUtils.trim(value));
}
@Override
public String convertToPresentation(String value, ValueContext context) {
if (value == null) {
return "";
}
return value;
}
};
AtomicInteger invokes = new AtomicInteger();
Validator<String> customRequiredValidator = (value, context) -> {
invokes.incrementAndGet();
if (value == null) {
return ValidationResult.error("Input required.");
}
return ValidationResult.ok();
};
binder.forField(textField).withConverter(stringBasicPreProcessingConverter).asRequired(customRequiredValidator).bind(Person::getFirstName, Person::setFirstName);
binder.setBean(item);
assertThat(textField.getErrorMessage(), isEmptyString());
assertEquals(1, invokes.get());
textField.setValue(" ");
assertNotNull(textField.getErrorMessage());
assertEquals("Input 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.converter.Converter in project flow by vaadin.
the class BinderTest method bindingReadBean_converterThrows_exceptionHandlerSet_bindingExceptionIsThrown.
@Test(expected = BindingException.class)
public void bindingReadBean_converterThrows_exceptionHandlerSet_bindingExceptionIsThrown() {
TestTextField testField = new TestTextField();
setExceptionHandler();
binder.forField(testField).withConverter(Converter.<String, String>from(name -> Result.ok(name), name -> {
throw new NullPointerException();
})).bind(Person::getFirstName, Person::setFirstName).read(new Person());
}
use of com.vaadin.flow.data.converter.Converter in project flow by vaadin.
the class BinderTest method readBean_converterThrows_exceptionHandlerSet_bindingExceptionIsThrown.
@Test(expected = BindingException.class)
public void readBean_converterThrows_exceptionHandlerSet_bindingExceptionIsThrown() {
TestTextField testField = new TestTextField();
setExceptionHandler();
binder.forField(testField).withConverter(Converter.<String, String>from(name -> {
throw new NullPointerException();
}, name -> name)).bind(Person::getFirstName, Person::setFirstName).read(new Person());
}
use of com.vaadin.flow.data.converter.Converter in project flow by vaadin.
the class RequiredFieldConfiguratorUtil method testConvertedDefaultValue.
/**
* Tests the converted default value of the provided binding builder if
* possible.
*
* @param binding
* the binding builder to test
* @param predicate
* predicate for testing the converted default value
* @return <code>true</code> if a converted default value is available and
* it passes the test; <code>false</code> if no converted default
* value is available or if it doesn't pass the test
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static boolean testConvertedDefaultValue(BindingBuilder<?, ?> binding, Predicate<Object> predicate) {
if (binding instanceof BindingBuilderImpl<?, ?, ?>) {
HasValue<?, ?> field = binding.getField();
Converter converter = ((BindingBuilderImpl<?, ?, ?>) binding).getConverterValidatorChain();
Result<?> result = converter.convertToModel(field.getEmptyValue(), BindingImpl.createValueContext(field));
if (!result.isError()) {
Object convertedEmptyValue = result.getOrThrow(IllegalStateException::new);
return predicate.test(convertedEmptyValue);
}
}
return false;
}
Aggregations