Skip to content

Commit e2e5674

Browse files
committed
Document databinding index validation
Document that indexed binding to arrays, collections, and many-ended associations requires non-negative integer indexes, that invalid indexes are reported as binding errors without mutating the target, and that map keys are unaffected. Align the Set binding example comment with the integer index requirement. Assisted-by: opencode:gpt-5.5
1 parent 91073c8 commit e2e5674

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

grails-doc/src/en/guide/theWebLayer/controllers/dataBinding.adoc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,15 @@ assert band.albums[1].numberOfTracks == 7
128128

129129
That code would work in the same way if `albums` were an array instead of a `List`.
130130

131+
NOTE: When binding to an array, a `Collection`, or a many-ended domain association by index, the value inside square brackets must be a non-negative integer. Entries such as `albums[-1]` or `albums[bogus]` are rejected as binding errors. The error field name includes the offending indexed segment, that binding path is skipped, and the target array, collection, or association is not changed by that entry. Map keys are not interpreted as numeric indexes, so keys such as `players[guitar]` remain valid map keys.
132+
131133
Note that when binding to a `Set` the structure of the `Map` being bound to the `Set` is the same as that of a `Map` being bound to a `List` but since a `Set` is unordered, the indexes don't necessarily correspond to the order of elements in the `Set`. In the code example above, if `albums` were a `Set` instead of a `List`, the `bindingMap` could look exactly the same but 'Foxtrot' might be the first album in the `Set` or it might be the second. When updating existing elements in a `Set` the `Map` being assigned to the `Set` must have `id` elements in it which represent the element in the `Set` being updated, as in the following example:
132134

133135
[source,groovy]
134136
----
135137
/*
136138
* The value of the indexes 0 and 1 in albums[0] and albums[1] are arbitrary
137-
* values that can be anything as long as they are unique within the Map.
139+
* non-negative integers that only need to be unique within the Map.
138140
* They do not correspond to the order of elements in albums because albums
139141
* is a Set.
140142
*/
@@ -516,7 +518,7 @@ class AccountingController {
516518
==== Data binding and type conversion errors
517519

518520

519-
Sometimes when performing data binding it is not possible to convert a particular String into a particular target type. This results in a type conversion error. Grails will retain type conversion errors inside the link:{domainClassesRef}errors.html[errors] property of a Grails domain class. For example:
521+
Sometimes when performing data binding it is not possible to convert a particular String into a particular target type. This results in a type conversion error. Grails will retain type conversion errors inside the link:{domainClassesRef}errors.html[errors] property of a Grails domain class. Invalid indexed array, collection, and many-ended association binding paths, such as negative or non-integer indexes, are also retained as binding errors, and the field name identifies the indexed path that was rejected. For example:
520522

521523
[source,groovy]
522524
----

0 commit comments

Comments
 (0)