【发布时间】:2014-02-25 11:59:39
【问题描述】:
我是 Grails 的新手,显然遗漏了一些东西。但是什么?!
我创建了一个带有 String 属性类别的 DomainClass An。在我定义的约束中,这个类别应该有多个(列表)值:
class An {
String category
static constraints = {
category nullable: true, inList:["do", "me", "a", "favour"]
}
}
在视图中显示为多选框:
<g:select name="category" from="${anInstance.constraints.category.inList}"
value="${anInstance?.category}"
valueMessagePrefix="a.category"
noSelection="${['': 'Please select one ...'}"
multiple="multiple" size="5"/>
保存方式为标准:
def save = {
def anInstance = new An(params)
if (anInstance.save(flush: true)){
flash.message = "${message(..)}"
redirect(action: "show", id: anInstance.id)
} else {
render(view: "create", model: [anInstance: anInstance])
}
}
当我只选择/保存一个值时,它会按预期选择/显示/保存。 当我想从此列表中选择/保存许多值时,我收到一条消息,即所选值不在列表中(default.not.inlist.message):
Property [category] of class [class An] with value [do, me, a, favour] is not contained within the list [[do, me, a, favour]].
感谢任何提示。
编辑:
正如猫先生所指出的,我的一个错误是将类别属性定义为String 而不是List<String>。现在选中的值显示为选中,但错误消息(default.not.inlist.message)仍然存在。
【问题讨论】:
-
能否粘贴返回的错误和执行保存操作的代码?
-
但是你为什么要尝试通过多选将字符串值列表设置为一个简单的字符串变量?
-
如果将约束更改为
inList:["do", "me", "a", "favour"].permutations() as List会发生什么?不过,您必须更改您的选择标签才能从其他地方获取其值。 -
我得到一个错误说约束 inList 应该实现接口
java.util.List。 -
这就是
as List的用途。如果您遇到该错误,您可能需要一些括号。
标签: grails constraints multi-select grails-constraints