我正在尝试使默认数据与具有域对象列表的命令对象一起工作。这是我创建的示例项目中的域类和命令对象,而不是我的最终域。
package testbinding
import grails.validation.Validateable
@Validateable
class SelectionCommand implements Serializable {
List<Book> books
Author author
}
与书籍和作者:
package testbinding
class Book {
Long id
String name
static constraints = {
}
}
package testbinding
class Author {
Long id
String name
static constraints = {
}
}
控制器:
def index(SelectionCommand command) {
println command
if (command?.hasErrors()) {
println command?.errors
}
[command: command]
}
如果我有一个使用书籍域索引的表单,则绑定是正确的。例如:
<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>
这可以正确绑定,但我需要 book 是一个下拉列表,所以我不能将它编入索引。
使用时:
<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>
我无法正确绑定。我已经尝试过,name="books"
并且name="books.id"
两者都出现了验证错误。
我的示例项目使用的是 Grails 2.3.9,但我在 2.3.11 中遇到了同样的问题。
这有一个老问题,但这应该在 2.3.x 中解决。