From c72e2a43cba9e769e9c6e83496155aab3354bcc4 Mon Sep 17 00:00:00 2001 From: Vaadin Bot Date: Thu, 16 Jan 2025 12:15:00 +0100 Subject: [PATCH] fix: fix setReadonly when using binder with records (#20855) (#20858) 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 --- .../com/vaadin/flow/data/binder/Binder.java | 5 +++-- .../com/vaadin/flow/data/binder/BinderTest.java | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java b/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java index a94abaefe51..5f0b498f866 100644 --- a/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java +++ b/flow-data/src/main/java/com/vaadin/flow/data/binder/Binder.java @@ -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"); } @@ -3535,7 +3535,8 @@ public boolean hasChanges(Binding 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)); } diff --git a/flow-data/src/test/java/com/vaadin/flow/data/binder/BinderTest.java b/flow-data/src/test/java/com/vaadin/flow/data/binder/BinderTest.java index 0c9b4c0d7cf..cdee582026f 100644 --- a/flow-data/src/test/java/com/vaadin/flow/data/binder/BinderTest.java +++ b/flow-data/src/test/java/com/vaadin/flow/data/binder/BinderTest.java @@ -1303,6 +1303,23 @@ public void setReadonlyShouldIgnoreBindingsWithNullSetter() { assertTrue("Age field should be readonly", ageField.isReadOnly()); } + @Test + public void setReadonly_record_allFieldsAreReadonly() { + Binder 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)