Reputation: 167
I am working on a project which uses a multiselect field.
My domain class is defined as:
class RoomWanted{
Set<MateAgeRange> mateAgeRanges
}
Then:
public enum MateAgeRange {
TWENTIES('18-29')
,THIRTIES('30-39')
,FOURTIES("40-49")
,FIFTIES("50-59")
,SIXTIES("60+")
final String value
private MateAgeRange(String value) {
this.value = value
}
String toString() { value }
String getKey() { name() }
static belongsTo=[roomWanted:RoomWanted]
}
My command object is:
class RoomWantedDetailsCommand implements Serializable {
Set<MateAgeRange> mateAgeRanges
}
I want to have the create page, edit page, show page
Here is my create page
<g:select name="mateAgeRanges" multiple="true" from="${MateAgeRange?.values()}" optionKey="key" />
My command object 'cmd' gets the value
print cmd.mateAgeRanges
gives the value [30-39, 50-59] when I select these two.
But it does not bind to the field in the domain class. In the show page, if I use cmd.mateAgeRanges it returns [].
Upvotes: 0
Views: 1030
Reputation: 167
The domain class should be defined as:
class RoomWantedDetailsCommand implements Serializable {
Set<MateAgeRange> mateAgeRanges
static hasMany = [ mateAgeRanges:MateAgeRange ]
}
Enum is defined as:
public enum MateAgeRange {
TWENTIES('18-29')
,THIRTIES('30-39')
,FOURTIES("40-49")
,FIFTIES("50-59")
,SIXTIES("60+")
final String value
private MateAgeRange(String value) {
this.value = value
}
String toString() { value }
String getKey() { name() }
static belongsTo=[roomWanted:RoomWanted]
}
In the edit page:
<g:select name="mateAgeRanges" multiple="true" from="${MateAgeRange?.values()}" optionKey="key" value="${roomWanted?.mateAgeRanges}"/>
In the show page: ${roomWanted?.mateAgeRanges }
In the create page
Upvotes: 1
Reputation: 20717
add hasMany to your RoomWanted
class
class RoomWanted{
Set<MateAgeRange> mateAgeRanges
static hasMany = [ mateAgeRanges:MateAgeRange ]
}
if you don't, GORM doesn't fill this association in full-auto.
Then you should use roomWanted.addToMateAgeRanges(..)
to fill-in the values
Upvotes: 0