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.

Check Box List

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).

6 Responses to “multi-select option for a has-many relationship : check box list”

  1. new bee Says:

    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

  2. msanjay Says:

    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.

  3. new bee Says:

    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

  4. Veena Shivanna Says:

    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.

  5. msanjay Says:

    veena this is not about .net but grails…

    Grails is an open source web application framework which leverages the Groovy programming language (which is in turn based on the Java platform). Grails is intended to be a high-productivity framework by following the “coding by convention” paradigm, providing a stand-alone development environment and hiding much of the configuration detail from the developer.

    Grails was previously known as ‘Groovy on Rails’ (the name was dropped in response to a request by David Heinemeier Hansson, founder of the Ruby on Rails framework.[1]). Work began in July 2005, with the 0.1 release on March 29, 2006 and the 1.0 release announced on February 18, 2008.

    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.

  6. msanjay Says:

    new bee, could you please attach relevant parts of the source code… thanks

Leave a Reply