我正在尝试使默认数据与具有域对象列表的命令对象一起工作。这是我创建的示例项目中的域类和命令对象,而不是我的最终域。
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 = {
}
}
Controller :
def index(SelectionCommand command) {
println command
if (command?.hasErrors()) {
println command?.errors
}
[command: command]
}
如果我有一个使用书籍域索引的表单,则绑定(bind)是正确的。例如:
<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>
这可以正确绑定(bind),但我需要 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>
我无法正确绑定(bind)。我试过
name="books"
和 name="books.id"
并且我得到验证错误。我的示例项目使用的是 Grails 2.3.9,但我在 2.3.11 中遇到了同样的问题。
有一个旧的issue为此,但这应该在 2.3.x 中解决。
最佳答案
回答我自己的问题。这显然在 Grails 2 到 2.4.4 (https://github.com/grails/grails-core/issues/1380) 中仍然是一个问题。
关于grails - 无法在没有索引的情况下将列表绑定(bind)到命令对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52674384/