私は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])
}
}
値を 1 つだけ選択/保存すると、期待どおりに選択/表示/保存されます。このリストから多くの値を選択/保存したい場合、選択した値がリストにないというメッセージが表示されました (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]].
どんなヒントでも大歓迎です。
編集:
Mr.Cat が指摘したように、私の間違いの 1 つは、カテゴリ プロパティを asString
と notで定義したことList<String>
です。選択した値が選択済みとして表示されるようになりましたが、エラー メッセージ (default.not.inlist.message) は残ります。