{"id":4381,"date":"2011-10-24T12:21:44","date_gmt":"2011-10-24T06:51:44","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=4381"},"modified":"2011-10-24T12:21:44","modified_gmt":"2011-10-24T06:51:44","slug":"grails-binddata-to-collections","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/grails-binddata-to-collections\/","title":{"rendered":"Grails bindData to collections"},"content":{"rendered":"<p>Recently, I was stuck with a scenario where I was trying to bind a list of objects in my controller.  While trying the way as suggested in the grails docs I was getting some weird IndexOutOfBoundException. Luckily I found a good solution on the grails mailing list: <a href=\"http:\/\/grails.1312388.n4.nabble.com\/Databinding-Collection-of-non-domain-objects-tp3260578p3260856.html\">http:\/\/grails.1312388.n4.nabble.com\/Databinding-Collection-of-non-domain-objects-tp3260578p3260856.html<\/a><br \/>\nThanks to <a href=\"http:\/\/grails.1312388.n4.nabble.com\/template\/NamlServlet.jtp?macro=user_nodes&amp;user=210908\">mkwhit<\/a> for asking this and   <a href=\"http:\/\/grails.1312388.n4.nabble.com\/template\/NamlServlet.jtp?macro=user_nodes&amp;user=137554\">Dana<\/a> for suggesting this solution.<br \/>\n<br \/>\nThere is  a Car class in my src\/groovy. There is a create.gsp that should create n number of cars at a time. The problem is to bind this data to a List of Car objects.<\/p>\n<p>Here is how I got it done:<\/p>\n<p>I ensured in my gsp that the car parameters are passed as car.1.name, car.1.brand, car.2.name, car.2.brand &#8230;.. So, when I did a params.car I get a Map like this:<br \/>\n[groovy]<br \/>\n1: [name:&#8217;carA&#8217;, brand:&#8217;brand1&#8242;]<br \/>\n2: [name:&#8217;carB&#8217;, brand:&#8217;brand2&#8242;]<br \/>\n3: [name:&#8217;carC&#8217;, brand:&#8217;brand2&#8242;]<br \/>\n1.name:&#8217;carA&#8217;<br \/>\n1.brand:&#8217;brand1&#8242;<br \/>\n2.name:&#8217;carB&#8217;<br \/>\n2.brand:&#8217;brand2&#8242;<br \/>\n3.name:&#8217;carC&#8217;<br \/>\n3.brand:&#8217;brand3&#8242;<br \/>\n[\/groovy]<\/p>\n<p>[groovy]<br \/>\ndef carParams = params.car.values().findAll {it instanceof GrailsParameterMap}<br \/>\nList&lt;Car&gt; carList = carParams.collect {<br \/>\n   def car = new Car()<br \/>\n   bindData(car, it)<br \/>\n   car<br \/>\n}[\/groovy]<\/p>\n<p>So, we have a list of Car objects out from the params.<\/p>\n<p>Hope this helps.<\/p>\n<p>-Mohd Farid-<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, I was stuck with a scenario where I was trying to bind a list of objects in my controller. While trying the way as suggested in the grails docs I was getting some weird IndexOutOfBoundException. Luckily I found a good solution on the grails mailing list: http:\/\/grails.1312388.n4.nabble.com\/Databinding-Collection-of-non-domain-objects-tp3260578p3260856.html Thanks to mkwhit for asking this and [&hellip;]<\/p>\n","protected":false},"author":26,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[7],"tags":[361,665,664,4840],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4381"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/26"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=4381"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4381\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=4381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=4381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=4381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}