multi-select option for a has-many relationship : check box list
[TODO: open a new site for technical posts or revamp this one so that they don’t get mixed up…!!]
After a long time, thanks to the lovely spirit, opportunity and encouragement I received from my fellow spiders @ spiderlogic, I actually managed to contribute something however insignificant to the GRAILS open-source community.
For a has-many relationship where we need to select one of the existing
options – here is a new checkBoxList tag that can be used.
Label List
tag can be used as well to show the selected items (though
eventually I just added a helper method in the domain object to get the
list as a string).

hi, I am using the checkboxlist tag in my application. I am getting a list of Id’s that are selected but I need to convert them to domain objects. Can you please let me know how I can get the corresponding domain objects.
Thank you,
new bee
hi new bee, as explained in the docs…
To map the selected ids to corresponding domain objects, an additional set property required in the containing domain class
// this is a has-many defined as usual
static hasMany = [users:User]
// this additional setter is used in the multiselect list to convert
// the ids selected in the checkbox list to the corresponding domain objects
public void setUserIds(List ids) {
this.users = ids.collect { User.get(it) }
}
Hope that works for you, let me know in case of any issues.
Thanks for the reply Sanjay.
I inclued the setproperty in the domain class but I am getting this error –
builder.ClosureInvokingAction Exception occured invoking flow action: No signature of method: gov.pacts.domain.lookup.LookupType.lookupIds() is applicable for argument types: ([Ljava.lang.String;) values: {[“2”, “3”, “5”]}
groovy.lang.MissingMethodException: No signature of method: gov.pacts.domain.lookup.LookupType.lookupIds() is applicable for argument types: ([Ljava.lang.String;) values: {[“2”, “3”, “5”]}
I tried converting the array to a list by using –
List lookupList = params.lookupIds.toList()
but got this error –
No signature of method: gov.pacts.domain.lookup.LookupType.lookupIds() is applicable for argument types: (java.util.ArrayList) values: {[“2”, “3”, “5”]}
Am I missing anything? any help with this..
Thank you,
new bee
Sanjay, for some reason the links are not working.
Incidentaly I was working with CheckedListbox and found it was infact not possible to tag a value and a display member like we do that for an ordinary checkbox. I finally wrote an algorithm in the derived class to get that done.
Well, such small properties do come handy at times. Pls check the links when you get a chance.
veena this is not about .net but grails…
The links are fine, maybe its blocked on your proxy or something. Just google for checkboxlist grails and you’ll get it from google directly.
new bee, could you please attach relevant parts of the source code… thanks