Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use binding object to check for Binder#hasChanges #17861

Merged
merged 14 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,18 @@ default BindingValidationStatus<TARGET> validate() {
* @return A boolean value
*/
boolean isConvertBackToPresentation();

/**
* Checks whether the field that the binding uses has uncommitted
* changes.
*
* @throws IllegalStateException
* if the binding is no longer attached to a Binder.
*
* @return {@literal true} if the field the binding uses has uncommitted
* changes, otherwise {@literal false}.
*/
boolean hasChanges();
}

/**
Expand Down Expand Up @@ -1589,6 +1601,16 @@ private <T> T execute(Supplier<T> supplier) {
}
}
}

@Override
public boolean hasChanges() throws IllegalStateException {
if (this.binder == null) {
throw new IllegalStateException(
"This Binding is no longer attached to a Binder");
}

return this.binder.hasChanges(this);
}
}

/**
Expand Down Expand Up @@ -3034,6 +3056,17 @@ public boolean hasChanges() {
return !changedBindings.isEmpty();
}

/**
* Checks whether a bound field has uncomitted changes.
*
* @param binding
* Binding to be checked
* @return true if field has been changed.
*/
public boolean hasChanges(Binding<BEAN, ?> binding) {
return hasChanges() && changedBindings.contains(binding);
}

/**
* Sets the read only state to the given value for all currently bound
* fields.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThrows;

public class BinderTest extends BinderTestBase<Binder<Person>, Person> {

Expand Down Expand Up @@ -199,6 +200,75 @@ public void clearBean_setsHasChangesToFalse() {

}

@Test
public void bindingHasChanges_trueWhenFieldValueChanges() {
var binding = binder.forField(nameField).bind(Person::getFirstName,
Person::setFirstName);

binder.readBean(item);
assertEquals("No name field value", "Johannes", nameField.getValue());
assertFalse("Field marked as changed after reading bean",
binder.hasChanges(binding));

benedikt-roth marked this conversation as resolved.
Show resolved Hide resolved
ageField.setValue("99");
assertFalse("Age field caused name field change",
binder.hasChanges(binding));

nameField.setValue("James");
assertTrue("Binder did not have value changes",
binder.hasChanges(binding));

binder.readBean(null);

assertFalse("Binder has changes after clearing all fields",
binder.hasChanges(binding));

}

@Test
public void bindingInstanceHasChanges_trueWhenFieldValueChanges() {
var binding = binder.forField(nameField).bind(Person::getFirstName,
Person::setFirstName);

binder.readBean(item);

assertEquals("No name field value", "Johannes", nameField.getValue());
assertFalse("Field marked as changed after reading bean",
binding.hasChanges());

ageField.setValue("99");
assertFalse("Age field caused name field change", binding.hasChanges());

nameField.setValue("James");
assertTrue("Binder did not have value changes", binding.hasChanges());

binder.readBean(null);

assertFalse("Binder has changes after clearing all fields",
binding.hasChanges());
}

benedikt-roth marked this conversation as resolved.
Show resolved Hide resolved
@Test
public void bindingInstanceHasChanges_throwsWhenBinderNotAttached() {
var binding = binder.forField(nameField).bind(Person::getFirstName,
Person::setFirstName);

binder.readBean(item);

assertFalse("Field marked as changed after reading bean",
binding.hasChanges());

nameField.setValue("James");
assertTrue("Binder did not have value changes", binding.hasChanges());

binding.unbind();

assertThrows("Expect unbound binding to throw exception",
IllegalStateException.class, () -> {
binding.hasChanges();
});
}

@Test
public void clearReadOnlyBinder_shouldClearFields() {
binder.forField(nameField).bind(Person::getFirstName,
Expand Down
Loading