Hello,
I’m trying to implement a Textfield without binding, converting String Input to Integer and displaying an error message to the user if the input cannot be converted.
I startet out with a validate-Method, as proposed in the forum here:
https://vaadin.com/forum#!/thread/15426235/15427287
private Result<Integer> validateAndConvert(String value) {
return new StringToIntegerConverter(0, "Input must be an Integer").convertToModel(value,
new ValueContext(Locale.getDefault()));
}
Then I wired a ValueChangeListener to the method:
filterField.addValueChangeListener(e -> {
Result<Integer> result = validateAndConvert(e.getValue());
if (result.isError()) {
UserError error = new UserError(result.getMessage().orElse("no error message"));
filterField.setComponentError(error);
} else {
}
});
Now, to finally get the successfully converted user input, I have to use the getOrThrow-Method, which, as far as I can tell, involves defining my own exception-provider should the conversion fail.
This seem massive overkill for my use case, but without a concise example I get lost in the API. Is there a more simple way to do this?