Required text field if checkbox is checked

Hey,
I’ve a binder that has a checkbox. If the checkbox is checked a TextField is shown.
This TextField should be required if the checkbox is checked. The only way I’ve seen to do this is using setAsRequiredEnabled on an existing binding. As per source code this sadly instantly validates the form https://github.com/vaadin/flow/blob/49d68c756df67e25ff125d838f45cba6a073d7f7/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java#L1539. This leads to a pretty bad UI/UX for the user who checks the checkbox and instantly sees a big red box. Is there any way around it?

Personally, I remove the old binding of the text field and create a new one with the correct required state, this shouldn’t call any validator

I tried that too but I had problems with binder.readBean(null) then. It ended up in a ConcurrentModificationException. I think that’s the case because the readBean tried to clear the fields and the value change listener for the checkbox is called which tried to remove the binding. I may be wrong tho.

I don’t use readBean, so I can’t really comment on that behavior. :sweat_smile:

How else do you clear the form?

I’m using setBean with an empty bean

hmm validation on the checkboxgroup is still called even if I use setBean

Oh Checkboxgroup… that’s an important info… I remember that there it works (not sure if intended) like this… that I can sadly confirm :grimacing: I haven’t digged deeper if bug or something else… it’s probably worth to create a issue in the flow-components repo

yeah sorry I wanted to make the case more simple and wrote the example with checkbox only

but thanks, will open up an issue

Checkboxgroup + RadioButtonGroup have this behavior :confused:

funny I just created a repro example and on the second read/setBean it removed the required label again

Lets see what this brings up https://github.com/vaadin/flow-components/issues/5399