vaadin / flow

Vaadin Flow is a Java framework binding Vaadin web components to Java. This is part of Vaadin 10+.
Apache License 2.0
600 stars 165 forks source link

Allow Converter to report an error and set a value #9483

Open nbabb opened 3 years ago

nbabb commented 3 years ago

There are cases where we want to both report an error and affect the value set in a component. More specifically we have a value in a component that, when selected, is a representation of other values. There are cases where selecting this value isn't appropriate and we want to a) report on this as a validation error and b) affect the value received by the component.

Currently SimpleResult seems to expressly forbid this: https://github.com/vaadin/flow/blob/907cbbd59612ac2e31daf5c1566fb48529d75200/flow-data/src/main/java/com/vaadin/flow/data/binder/SimpleResult.java#L50-L51

Ideally what I'd want to see is an error method that accepts both a value and a message, allowing me to affect the value displayed in the component (perhaps canceling the selection) while also displaying a validation warning.

static <R> Result<R> error(R value, String message)

Thanks

mshabarov commented 3 years ago

Thanks for issue, @nbabb ! Having a value and a warning message present in the Result makes sense maybe. But having a value along with an error seems semantically wrong. For a better understanding of what you want to achieve, could you please provide a code snippet showing your use case?

nbabb commented 3 years ago

I ended up implementing my requirement (to transpose a combo selection from one value into other values, for example an option of "All Favorite Foods" turns into "Apple Pie, Turkey, Stuffing") at the component level, as I realized later that the converter is between the bean and the UI, and so didn't support my use case.

However I do think that there might still be merit to my original proposal. One thing I had wanted the ability to do was to use the backing bean as a proxy for UI state, i.e. always have the bean represent selections from the UI regardless of whether they are valid. I was made aware of this api, Binder.writeBeanAsDraft, which allows you to write the UI contents to a Bean regardless of the validation state. I ended up not being able to do this due to our use of JSR-303 annotations which was causing the beans to throw errors when being written to with invalid state. But other users of Vaadin api's might wish to do this.

So the potential use-case might be for someone using the writeBeanAsDraft api, in the context of a converter, to write changes to the bean even when there is an error/warning.

I am not blocked by this inability, so feel free to prioritize (or close) this change request as you see fit. Thanks!