如何从Grails中的inList约束中选择多个值? [英] How can I select multiple values from the inList constraint in Grails?
问题描述
我创建了一个带有String属性类别的DomainClass An。在定义的约束中,此类别应该有多个(列表)值:
类An {
String category
static constraints = {
category nullable:true,inList:[do,me,a,favor]
}
}
在视图中显示为多个选择框:
< g:select name =categoryfrom =$ {anInstance.constraints.category.inList}
value =$ {anInstance?.category}
valueMessagePrefix =a.category
noSelection =$ {[''':'请选择一个...'}
multiple =multiplesize =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):
值[do,me,a,favor]的类[class An]的属性[category]不包含在列表中[[do,me,a,favor ]]。
感谢任何提示。
编辑:
正如Mr.Cat指出的,我的一个错误是将category属性定义为 String
,而不是 List< String>
。现在选定的值显示为已选择,但错误消息(default.not.inlist.message)仍然保留。
在选择框中选择多个项会导致在控制器中得到一个字符串列表,然后你试图将此列表存储在一个明显错误的字符串字段,具体不传递您的约束
I'm new to Grails and obviously missing something out.. but what?!
I created a DomainClass An with a String property category. In the constraints I defined, that this category should have multiple (list) values:
class An {
String category
static constraints = {
category nullable: true, inList:["do", "me", "a", "favour"]
}
}
In the view it is shown as a multiple select box:
<g:select name="category" from="${anInstance.constraints.category.inList}"
value="${anInstance?.category}"
valueMessagePrefix="a.category"
noSelection="${['': 'Please select one ...'}"
multiple="multiple" size="5"/>
The save method is standard:
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])
}
}
When I select/save just one value, it is selected/shown/saved as expected. When I want to select/save many values from this list, I got the message that the seleced values are not in the list (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]].
Any hint is appreciated.
EDIT:
As Mr.Cat pointed out, one of my mistakes was to define the category property as String
and not List<String>
. Now the selected values are shown as selected, but the error message (default.not.inlist.message) still remains.
selecting multiple items in select box causes that in the controller you get a list of strings, and then you are trying to store this list in a single String field which is obviously wrong, and specifically doesnt pass your costraint
这篇关于如何从Grails中的inList约束中选择多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!