I’m using new binder and I don’t get the behavior of its
hasChanges() method.
I’m binding a form field to binder (using
MVP pattern and
HasValues in
binder.bind() method in presenter). Field bounds correctly and takes value from binder and displays value in form field. Then I add event handling on the field in the view:
this.shortname.addValueChangeListener(e -> this.presenter.onChanges());
Then I want to make some stuff in presenter judging by hasChanges() status:
public void onChanges() {
if (this.binder.hasChanges()) {
// some stuff
}
}
But
hasChanges() method gives me
false when I type in the field. So, bound field value is
definitely changed (valuechange event triggered) but binder doesn’t have changes. How is that possible?
Assuming you have bound a bean to the binder with setBean, hasChanges returns false in this case because the changes made to the field have already been committed to the bound bean. If no bean would be set to the binder, or the change could not be committed to the bound bean due to a validation error, hasChanges would instead return true.
So, are you saying that
hasChanges() isn’t really about changes? Because method description says "
Check whether any of the bound fields’ values have changed since last
explicit call to setBean(Object), readBean(Object), removeBean(), writeBean(Object) or writeBeanIfValid(Object). ". Values
are changed
after setBean() and
none of the other methods were used.
This is a simple test app with a grid and a side form. When I select row on a grid I use a
setBean() method to fill a side form (binder) from a bean that I get from a row. So
I clearly see that bean in the binder is definitely set (form is filled correctly and I see it). Then I make changes to one of the form fields. I use
ValueChangeListener on fields, so I see that changes
are made . Then I check
hasChanges - it returns
false . Then I check
binder.validate().isOk() - it returns
true .
So, bean is set and changes to binder’s fields are made
after setBean() and validation doesn’t show errors. I still don’t get why hasChanges() doesn’t show changes.
hasChanges returns whether there are uncommitted changes. In your case all the changes made have already been committed to the bean before your call to
hasChanges , thus returning false. If no bean were set, it would instead return true in this case.
Aleksi,
Thanks for clarification on how it works in current implementation! I will try to use some other way to achieve my goals then.
Still, I have to say that description of hasChanges() clearly states that it should work differently:
You’re saying that my changes are commited
implicitly after setBean. But method’s description says “Check whether any of the bound fields’ values have changed since last
explicit call to setBean(Object), readBean(Object), removeBean(), writeBean(Object) or writeBeanIfValid(Object).”
So I assume that description is wrong and misleading.
Thanks for help anyway!
Hi,
I would like to know how I can detect if any e.g. TextField has change its value. There are situations, where current value of TextField is changed by the user and then restored to previous value and I would like for Vaadin to show me this as no change. Eg:
TextField current value is 10;
User change it to 15, after some time restore to 10 (manually, by typing value again).
Then he saves changes. What is easiest way to check if value has changed (in this case value has not changed).