Skip to content

Commit

Permalink
fix: fix setReadonly when using binder with records (#20855) (#20858)
Browse files Browse the repository at this point in the history
When Binder is used with record, calling setReadonly method does not have
any effect because the logic only considers bindings with a proper setter.
This change fixes the setReadonly logic to take care of all bindings when
Binder is using a record.

Fixes #20854

Co-authored-by: Marco Collovati <[email protected]>
  • Loading branch information
vaadin-bot and mcollovati authored Jan 16, 2025
1 parent 277fb35 commit c72e2a4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1659,7 +1659,7 @@ private void convertAndSetFieldValue(TARGET modelValue) {

@Override
public void setReadOnly(boolean readOnly) {
if (setter == null && !readOnly) {
if (!binder.isRecord && this.setter == null && !readOnly) {
throw new IllegalStateException(
"Binding with a null setter has to be read-only");
}
Expand Down Expand Up @@ -3535,7 +3535,8 @@ public boolean hasChanges(Binding<BEAN, ?> binding) {
* to set them to read-write
*/
public void setReadOnly(boolean readOnly) {
getBindings().stream().filter(binding -> binding.getSetter() != null)
getBindings().stream()
.filter(binding -> isRecord || binding.getSetter() != null)
.forEach(field -> field.setReadOnly(readOnly));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,23 @@ public void setReadonlyShouldIgnoreBindingsWithNullSetter() {
assertTrue("Age field should be readonly", ageField.isReadOnly());
}

@Test
public void setReadonly_record_allFieldsAreReadonly() {
Binder<TestRecord> binder = new Binder<>(TestRecord.class);
binder.forField(nameField).bind("name");
binder.forField(ageField).bind("age");

binder.getBinding("name").ifPresent(b -> b.setReadOnly(true));
binder.setReadOnly(true);
assertTrue("Name field should be readonly", nameField.isReadOnly());
assertTrue("Age field should be readonly", ageField.isReadOnly());

binder.setReadOnly(false);
assertFalse("Name field should not be readonly",
nameField.isReadOnly());
assertFalse("Age field should not be readonly", ageField.isReadOnly());
}

@Test
public void isValidTest_bound_binder() {
binder.forField(nameField)
Expand Down

0 comments on commit c72e2a4

Please sign in to comment.