Value conversion without binders

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?

Maybe something like this:

public class VaadinUI extends UI {

    private Integer value = null;

    @Override
    protected void init(VaadinRequest vaadinRequest) {

        TextField textField = new TextField("Type a integer");
        textField.addValueChangeListener(e -> {
            try {
                value = Integer.parseInt(e.getValue());
                textField.setComponentError(null);
            } catch (NumberFormatException ex) {
                textField.setComponentError(new UserError("That's not a integer"));
            }
        });

        setContent(new VerticalLayout(textField));
    }
}

Thank you Alejandro. I did something like that, just thought I could use the converters and validators.