Grails multiple select binding

When first time I try to use html select tag with multiple selection, I spent about 1h to figure "how to" because user guide is, hmmm, as is. So, there is my little example:

Lets create domain objects:

  1.  
  2. Author {
  3. String firstName
  4. String lastName
  5. static hasMany = [books:Book]
  6. }
  7.  
  1.  
  2. String title
  3. static belongsTo = [author:Author]
  4. }
  5.  

Author form will contain select with books like this:

  1.  
  2. <g:select optionKey="id" optionValue="title" multiple="multiple" name="author.books" id ="author_books" from="${allBooks}" value="${authorInstance?.books*.id}"/>
  3.  

Multiple select binding to Author.books is now available.

2 Comments

RSS feed for comments on this post.

  1. In your save closure, are you able to create an instance of Author by using something like

    def auth = new Author(params)

    or is something needed to manually set the relationship? I cannot get it to work.

    Comment by Weasel — January 14, 2010 #

  2. Yes, for my its working. I have a little more complicated situation (Product (*) -> (*)[Color,Size]), but binding is working. In my case I used namespaces for parameters.

    Comment by admin — January 14, 2010 #

Sorry, the comment form is closed at this time.

Powered by WordPress with GimpStyle Theme design by Horacio Bella.
Entries and comments feeds. Valid XHTML and CSS.