I need to add a reaction in my interface on changes in TextField and ComboBox.
Binder is a convinient way for handling data binding in Vaadin.
I can't use on textField.addValueChangeListener(...)
as it is called before new value is set in binded model.
Is there a way to add a something like a value change listener on specific field in binder?
I can use binder.addValueChangeListener()
but it is fired on every bound field changes and I can't easily tell them apart.
Ideally, it would be something like that:
binder.forField(specificTextField)
.withConverter(...)
.withValidator(...)
.bind(Person::getName, Person::setName)
.addValueChangeListener(...); // nonexistent functional
The answer to the question whether there is a way to add a value change listener to a single Binding itself is no there is not. I agree this could be a cool and useful addition to the Flow API. Please go ahead and write an issue in their github
However, as you noticed yourself, there is the method Binder::addValueChangeListener
, and there is a way to tell them apart. The accepted eventListener has a way to get the HasValue
(a.k.a. bound input field) that fired the change event. it also has access to both the old and the new value.
TextField tf = new TextField();
ComboBox cb = new ComboBox();
// todo: binding of fields
binder.addValueChangeListener(valueChangeListener -> {
if(valueChangeListener.getHasValue().equals(tf)){
// tf value changed
} else if(valueChangeListener.getHasValue().equals(cb)) {
// cb value changed
}
}