Skip to content

Commit 02dc13c

Browse files
authored
Update components-binder-load.adoc (#4295)
Improve the documentation of the Binder with a Record class
1 parent 00e77eb commit 02dc13c

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

articles/flow/binding-data/components-binder-load.adoc

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,27 @@ submitButton.addClickListener(event -> {
211211

212212
== Using Java Records with Binder
213213

214-
Instead of using Java Beans, it's possible to use the [classname]`Binder` with Java Records. Since Java Records are immutable, only manual reading and writing can be used when the datatype is a record. This means that methods relying on bean datatypes -- such as `writeBeanAsDraft`, `writeBean`, and `setBean` -- throw an exception when called for a Binder with a record datatype. Additionally, since records can only be read via `readBean`, the `refreshFields` method clears all of the Binder fields.
214+
Instead of using Java Beans, it's possible to use the [classname]`Binder` with Java Records. Since Java Records are immutable, only manual reading and writing can be used when the datatype is a record. This means that methods relying on bean datatypes -- such as `writeBeanAsDraft`, `writeBean` and `setBean` -- throw an exception when called for a Binder with a record datatype. Additionally, the `refreshFields` method clears all of the Binder fields.
215215

216-
For reading a record, the `readRecord` method should be used as shown in the example below.
216+
You have to use reflection based on record property names to bind values.
217+
To use reflection, create a Binder by providing the record class, for example `new Binder<>(Person.class);`.
218+
You can't bind the field based on setters.
219+
220+
For reading a record, the `readRecord` or `readBean` method should be used as shown in the example below.
217221

218222
[source,java]
219223
----
220224
public record Person(String firstName, String lastName) {}
221225
226+
Binder<Person> binder = new Binder<>(Person.class);
227+
// Bind based on property names
228+
binder.bind(firstNameField, "firstName");
229+
binder.bind(lastNameField, "lastName");
230+
222231
binder.readRecord(new Person("John", "Doe"));
223232
----
224233

225-
This method throws an exception, if used with beans.
234+
The `readRecord` method throws an exception, if used with beans.
226235

227236
For writing a record, the `writeRecord` method should be used. Calling this method applies field and binder level validators, and either returns a new record instance with the current state of the binder, or throws a `ValidationException`.
228237

0 commit comments

Comments
 (0)