grails - 无法在没有索引的情况下将列表绑定(bind)到命令对象

标签 grails command-objects

我正在尝试使默认数据与具有域对象列表的命令对象一起工作。这是我创建的示例项目中的域类和命令对象,而不是我的最终域。

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/

相关文章:

javascript - 在grails中发送错误并用Javascript读取

grails - Grails应用程序会自行杀死

grails 项目在没有互联网连接的情况下无法运行

grails - Grails命令对象是否支持多个MultipartFile?

validation - Grails 重复错误消息

grails - 错误:找不到 namespace 的标签库:g

grails - 如果索引大于 255,则数据绑定(bind)不起作用

grails - 命令对象仅在 Controller 中,还是可以传递给服务层?

spring - 在 Grails 的命令对象中获取 session

ajax - Grails,我该如何从 Controller 对AJAX调用发出错误响应