I'm trying to make the default data biding to work with a command object that has a domain object list. This are domain classes and command objects from an example project I created not my final domains.
package testbinding
import grails.validation.Validateable
@Validateable
class SelectionCommand implements Serializable {
List<Book> books
Author author
}
With Book and Author:
package testbinding
class Book {
Long id
String name
static constraints = {
}
}
package testbinding
class Author {
Long id
String name
static constraints = {
}
}
Controller:
def index(SelectionCommand command) {
println command
if (command?.hasErrors()) {
println command?.errors
}
[command: command]
}
If I have a form using indexes for the book domain the binding is correct. For example:
<label>Books</label>
<input name="book[0].id" value="1"/>
<input name="book[1].id" value="2"/>
<label>Author</label>
<g:select name="author.id" value="${1L}" from="${Author.list()}" optionKey="id" optionValue="name"/>
<button type="submit">Submit</button>
This binds correctly but I need book to be a drop-down so I can't have it indexed.
When using:
<label>Books</label>
<g:select name="books" from="${Book.list()}" multiple="true" optionKey="id" optionValue="name" value="${[1L, 2L]}"/>
<label>Author</label>
<g:select name="author.id" value="${1L}" from="${Author.list()}" optionKey="id" optionValue="name"/>
<button type="submit">Submit</button>
I'm not able to get the binding correctly. I've tried with name="books"
and name="books.id"
and with both I get validation errors.
My sample project is using Grails 2.3.9 but I had the same issues in 2.3.11.
There is an old issue for this but this was supposed to be solved in 2.3.x.
Answering my own question. This apparently was still an issue in Grails 2 up to 2.4.4 (https://github.com/grails/grails-core/issues/1380).