Unbind a field from Binder in Vaadin 8

Hello,
is it possible to unbind a field from the binder dynamically? I think with the old BeanFieldGroup it was also possible to unbind a bound field.
My use case is, that if a decision was choosen in a ComboBox additional fields will be shown, based on the current decision. But if the user selects another decision, the already bound fields throw an error by validation if the old bound fields have e. g. a required flag. (For example the unvisible Field is required and still bound to Bean-Field, the validation fails as expected)
Thanks!

Hi,

might be that it’s not doable. Can you create a small code snippet that demonstrates the use case?

-Olli

Perhaps you can do your own validation ? (if this item is selected then the additional fields will be shown and required else the fields will be cleared and should be null)

I was looking for a way to do this too. I have a large data set where there are a lot of inherited classes that have only a few fields between them so I wan’t keen on creating a display for each one.

I ended up using a few HashMaps; a ValueChangeListener HashMap and a Textfield HashMap.

On component creation add all the textfields, one for each property, to the HashMap.

Then when a selection event occured, set all current textfield visibilities to false, remove any valuechange listeners, then create new binders for each property in the newly selected data object (I used reflection to get the field parameter from my data object to set the converter), then create a new ValueChangeListener with the new binder.writeTo(Object) and store the Registration so you can remove it when the next selection occurs. That way the old binder is never called.

Then finally for each field in the new selection I set the visibility of the textfields to true.

Keen to know if there’s a better way.

Also use this heavily in Vaadin 7 projects, similar use-case. A dropdown (or other selection mechanism) alters which fields are shown to the user, when the user changes this dropdown the previous shown fields are removed from the form, so they should also be removed from the binder.

@Hayden, it might be unnecessary to remove all the listeners, but make them to respect which properties are required to be updated for the current item.
And I think there is no better way for the moment. Creating a new Binder isn’t really heawyweight operation, and removing bunch of fields from there might be worse than new Binder creation.

Hi,

A fix to add this functionality to Binder was
just merged
to the Vaadin 8 master branch. It will be available in an upcoming Vaadin 8 release soon.

//Teemu

Hey Teemu,

thank you so much for the fix! Just another question; how would you remove just an asRequired validation? Would it be enough to use field.setRequired(false)? Or do you see an use case to have such a function in the binder next to unbind?

Best regards,
Knoobie

HI Knoobie,

The
asRequired(String)
is just a short-hand for adding a
Validator
that checks !Objects.equals(value, field.getEmptyValue()) You can make your own customised version of it that checks also if the field returns
true
for
isRequiredIndicatorVisible()
.

if (field.isRequiredIndicatorVisible()) { return !Objects.equals(value, field.getEmptyValue()); } return true; Using the code-snippet above as your validator gives you control over the
asRequired
validation through
HasValue#setRequiredIndicatorVisible
.

//Teemu