|
Hey Grails crowd. This question might have been asked before, but this
morning I'm too lazy to search the list archive. So here it is. Is there a way to bind onto a collection of objects from GSP in controllers and perhaps using GPath expressions to also bind into nested object graphs. If so, can some one post an example. Sorry if this has been answered before. Regards, Dmitriy. --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
|
The bind mechanism uses Spring, and according to this:
http://static.springframework.org/spring/docs/2.0.x/reference/validation.html#beans-beans-conventions you can use "personList[0].firstname" to reference the firstname of person[0]. So in your HTML you'd have, say: <input name="personList[0].firstname" value="Fred" /> <input name="personList[0].surname" value="Jones" /> <input name="personList[1].firstname" value="Bob" /> <input name="personList[1].surname" value="Smith" /> Then in your controller: bind(form, params) where "form" is an instance of: class MyForm { def personList = [new Person(), new Person()] } But you might have to pre-initialise the list with a known size and object type as above. Not tried it, so if you use this approach, let us know how it goes! Dmitriy Kopylenko wrote: > Hey Grails crowd. This question might have been asked before, but this > morning I'm too lazy to search the list archive. So here it is. > > Is there a way to bind onto a collection of objects from GSP in > controllers and perhaps using GPath expressions to also bind into > nested object graphs. If so, can some one post an example. > > Sorry if this has been answered before. > > Regards, > Dmitriy. > > --------------------------------------------------------------------- > To unsubscribe from this list please visit: > > http://xircles.codehaus.org/manage_email > > > --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
|
Thanks we'll try this approach.
Dmitriy. Maurice Nicholson wrote: > The bind mechanism uses Spring, and according to this: > > http://static.springframework.org/spring/docs/2.0.x/reference/validation.html#beans-beans-conventions > > > you can use "personList[0].firstname" to reference the firstname of > person[0]. > > So in your HTML you'd have, say: > > <input name="personList[0].firstname" value="Fred" /> > <input name="personList[0].surname" value="Jones" /> > <input name="personList[1].firstname" value="Bob" /> > <input name="personList[1].surname" value="Smith" /> > > Then in your controller: > > bind(form, params) > > where "form" is an instance of: > > class MyForm { > def personList = [new Person(), new Person()] > } > > But you might have to pre-initialise the list with a known size and > object type as above. > > Not tried it, so if you use this approach, let us know how it goes! > > > Dmitriy Kopylenko wrote: >> Hey Grails crowd. This question might have been asked before, but >> this morning I'm too lazy to search the list archive. So here it is. >> >> Is there a way to bind onto a collection of objects from GSP in >> controllers and perhaps using GPath expressions to also bind into >> nested object graphs. If so, can some one post an example. >> >> Sorry if this has been answered before. >> >> Regards, >> Dmitriy. >> >> --------------------------------------------------------------------- >> To unsubscribe from this list please visit: >> >> http://xircles.codehaus.org/manage_email >> >> >> > > --------------------------------------------------------------------- > To unsubscribe from this list please visit: > > http://xircles.codehaus.org/manage_email > --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
|
In reply to this post by Maurice Nicholson old
Maurice,
this works like a charm! Thanks, Dmitriy. Maurice Nicholson wrote: > The bind mechanism uses Spring, and according to this: > > http://static.springframework.org/spring/docs/2.0.x/reference/validation.html#beans-beans-conventions > > > you can use "personList[0].firstname" to reference the firstname of > person[0]. > > So in your HTML you'd have, say: > > <input name="personList[0].firstname" value="Fred" /> > <input name="personList[0].surname" value="Jones" /> > <input name="personList[1].firstname" value="Bob" /> > <input name="personList[1].surname" value="Smith" /> > > Then in your controller: > > bind(form, params) > > where "form" is an instance of: > > class MyForm { > def personList = [new Person(), new Person()] > } > > But you might have to pre-initialise the list with a known size and > object type as above. > > Not tried it, so if you use this approach, let us know how it goes! > > > Dmitriy Kopylenko wrote: >> Hey Grails crowd. This question might have been asked before, but >> this morning I'm too lazy to search the list archive. So here it is. >> >> Is there a way to bind onto a collection of objects from GSP in >> controllers and perhaps using GPath expressions to also bind into >> nested object graphs. If so, can some one post an example. >> >> Sorry if this has been answered before. >> >> Regards, >> Dmitriy. >> >> --------------------------------------------------------------------- >> To unsubscribe from this list please visit: >> >> http://xircles.codehaus.org/manage_email >> >> >> > > --------------------------------------------------------------------- > To unsubscribe from this list please visit: > > http://xircles.codehaus.org/manage_email > --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
|
Excellent!
I thought a little more about it and (using the below example) "MyForm" could be a domain class or a plain Groovy object, as long as the collection property is an "array, list or other /naturally ordered/ collection" (from referenced Spring doc). I only mention it because the below example implies that MyForm is not a domain class. Dmitriy Kopylenko wrote: > Maurice, > > this works like a charm! > > Thanks, > Dmitriy. > > Maurice Nicholson wrote: >> The bind mechanism uses Spring, and according to this: >> >> http://static.springframework.org/spring/docs/2.0.x/reference/validation.html#beans-beans-conventions >> >> >> you can use "personList[0].firstname" to reference the firstname of >> person[0]. >> >> So in your HTML you'd have, say: >> >> <input name="personList[0].firstname" value="Fred" /> >> <input name="personList[0].surname" value="Jones" /> >> <input name="personList[1].firstname" value="Bob" /> >> <input name="personList[1].surname" value="Smith" /> >> >> Then in your controller: >> >> bind(form, params) >> >> where "form" is an instance of: >> >> class MyForm { >> def personList = [new Person(), new Person()] >> } >> >> But you might have to pre-initialise the list with a known size and >> object type as above. >> >> Not tried it, so if you use this approach, let us know how it goes! >> >> >> Dmitriy Kopylenko wrote: >>> Hey Grails crowd. This question might have been asked before, but >>> this morning I'm too lazy to search the list archive. So here it is. >>> >>> Is there a way to bind onto a collection of objects from GSP in >>> controllers and perhaps using GPath expressions to also bind into >>> nested object graphs. If so, can some one post an example. >>> >>> Sorry if this has been answered before. >>> >>> Regards, >>> Dmitriy. >>> >>> --------------------------------------------------------------------- >>> To unsubscribe from this list please visit: >>> >>> http://xircles.codehaus.org/manage_email >>> >>> >>> >> >> --------------------------------------------------------------------- >> To unsubscribe from this list please visit: >> >> http://xircles.codehaus.org/manage_email >> > > > --------------------------------------------------------------------- > To unsubscribe from this list please visit: > > http://xircles.codehaus.org/manage_email > > > --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
|
Actually the only way I was able to make it work is making "MyForm" a
domain class i.e.:
class MyForm { static transients = ["personList"] def personList = [] } otherwise it wasn't recognizing form.properties = params (it was throwing MissingPropertyException) ... and the Controller looks like this (that just a test/proof of concept for this binding stuff, not the real project): class PersonController { def index = { redirect(action:list,params:params) } ... def list = { def form = new MyForm() form.personList = Person.list( params ) session['myForm'] = form [ personList: form.personList ] } def update = { def form = session.myForm form.properties = params form.personList.each { if(it.delete) it.delete() else it.save() } form.personList = form.personList - form.personList.findAll { it.delete } render(view:'list',model:[personList:form.personList]) } } and list.gsp: ... <g:form controller="person" method="post" > <%i = 0%> <g:each in="${personList}" status="i"> <tr> <td><g:checkBox name='personList[${i}].delete' value="${it.delete}"></g:checkBox></td> <td><input type='text' name='personList[${i}].name' value="${it.name?.encodeAsHTML()}"/></td> <td class="actionButtons"> <span class="actionButton"><g:link action="show" id="${it.id}">Show</g:link></span> </td> </tr> </g:each> </tbody> </table> <div class="buttons"> <span class="button"><g:actionSubmit value="Update" /></span> </div> </g:form> ... Cheers, Dmitriy. P.S. I love Grails: I was able to prototype this from scratch (create new app, etc.) in the matter of minutes (5 minutes tops) :-) Maurice Nicholson wrote: Excellent! |
|
Yes the obj.properties = params only works with domain objects AFAIK.
But bind(target, params) works with anything. Thanks for the detailed feedback about how you solved this problem. It's all good for the mail archive :-) Dmitriy Kopylenko wrote: > Actually the only way I was able to make it work is making "MyForm" a > domain class i.e.: > > class MyForm { > > static transients = ["personList"] > > def personList = [] > } > > otherwise it wasn't recognizing *form.properties = params *(it was > throwing MissingPropertyException) > > ... and the Controller looks like this (that just a test/proof of > concept for this binding stuff, not the real project): > > class PersonController { > def index = { redirect(action:list,params:params) } > ... > > def list = { > def form = new MyForm() > form.personList = Person.list( params ) > session['myForm'] = form > [ personList: form.personList ] > } > > def update = { > def form = session.myForm > *form.properties = params* > form.personList.each { > if(it.delete) it.delete() > else it.save() > } > form.personList = form.personList - form.personList.findAll { > it.delete } > render(view:'list',model:[personList:form.personList]) > > } > } > > and list.gsp: > > ... > > <g:form controller="person" method="post" > > <%i = 0%> > *<g:each in="${personList}" status="i">* > <tr> > > *<td><g:checkBox > name='personList[${i}].delete' value="${it.delete}"></g:checkBox></td> > > <td><input type='text' > name='personList[${i}].name' value="${it.name?.encodeAsHTML()}"/></td>* > <td class="actionButtons"> > <span class="actionButton"><g:link > action="show" id="${it.id}">Show</g:link></span> > </td> > </tr> > </g:each> > </tbody> > </table> > <div class="buttons"> > <span class="button"><g:actionSubmit value="Update" > /></span> > </div> > </g:form> > ... > > Cheers, > Dmitriy. > > P.S. I love Grails: I was able to prototype this from scratch (create > new app, etc.) in the matter of minutes (5 minutes tops) :-) > > > Maurice Nicholson wrote: >> Excellent! >> >> I thought a little more about it and (using the below example) >> "MyForm" could be a domain class or a plain Groovy object, as long as >> the collection property is an "array, list or other /naturally >> ordered/ collection" (from referenced Spring doc). >> >> I only mention it because the below example implies that MyForm is >> not a domain class. >> >> >> Dmitriy Kopylenko wrote: >>> Maurice, >>> >>> this works like a charm! >>> >>> Thanks, >>> Dmitriy. >>> >>> Maurice Nicholson wrote: >>>> The bind mechanism uses Spring, and according to this: >>>> >>>> http://static.springframework.org/spring/docs/2.0.x/reference/validation.html#beans-beans-conventions >>>> >>>> >>>> you can use "personList[0].firstname" to reference the firstname of >>>> person[0]. >>>> >>>> So in your HTML you'd have, say: >>>> >>>> <input name="personList[0].firstname" value="Fred" /> >>>> <input name="personList[0].surname" value="Jones" /> >>>> <input name="personList[1].firstname" value="Bob" /> >>>> <input name="personList[1].surname" value="Smith" /> >>>> >>>> Then in your controller: >>>> >>>> bind(form, params) >>>> >>>> where "form" is an instance of: >>>> >>>> class MyForm { >>>> def personList = [new Person(), new Person()] >>>> } >>>> >>>> But you might have to pre-initialise the list with a known size and >>>> object type as above. >>>> >>>> Not tried it, so if you use this approach, let us know how it goes! >>>> >>>> >>>> Dmitriy Kopylenko wrote: >>>>> Hey Grails crowd. This question might have been asked before, but >>>>> this morning I'm too lazy to search the list archive. So here it is. >>>>> >>>>> Is there a way to bind onto a collection of objects from GSP in >>>>> controllers and perhaps using GPath expressions to also bind into >>>>> nested object graphs. If so, can some one post an example. >>>>> >>>>> Sorry if this has been answered before. >>>>> >>>>> Regards, >>>>> Dmitriy. >>>>> >>>>> --------------------------------------------------------------------- >>>>> To unsubscribe from this list please visit: >>>>> >>>>> http://xircles.codehaus.org/manage_email >>>>> >>>>> >>>>> >>>> >>>> --------------------------------------------------------------------- >>>> To unsubscribe from this list please visit: >>>> >>>> http://xircles.codehaus.org/manage_email >>>> >>> >>> >>> --------------------------------------------------------------------- >>> To unsubscribe from this list please visit: >>> >>> http://xircles.codehaus.org/manage_email >>> >>> >>> >> >> --------------------------------------------------------------------- >> To unsubscribe from this list please visit: >> >> http://xircles.codehaus.org/manage_email >> > --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
Would that it were so. I tried def obj = new MyObject() bind(obj,params) in a controller action and get the dreaded: No signature of method: LauncherController.bind() is applicable for argument types: ...etc. Where is the bind() method / closure defined? Is it broken, or am I out of luck? |
|
It's bindData().
Check out: http://grails.codehaus.org/Dynamic+Methods+Reference under Controller Methods -ck On Jul 12, 2007, at 10:26 AM, Dave Syer wrote: > > > > Maurice Nicholson wrote: >> >> Yes the obj.properties = params only works with domain objects AFAIK. >> >> But bind(target, params) works with anything. >> >> > > Would that it were so. I tried > > def obj = new MyObject() > bind(obj,params) > > in a controller action and get the dreaded: > > No signature of method: LauncherController.bind() is applicable for > argument > types: > ...etc. > > Where is the bind() method / closure defined? Is it broken, or am > I out of > luck? > > -- > View this message in context: http://www.nabble.com/Bind-a- > collection-of-objects-tf3187838.html#a11564794 > Sent from the grails - user mailing list archive at Nabble.com. > > > --------------------------------------------------------------------- > To unsubscribe from this list please visit: > > http://xircles.codehaus.org/manage_email > --------------------------------------------------------------------- To unsubscribe from this list please visit: http://xircles.codehaus.org/manage_email |
|
In reply to this post by Dmitriy Kopylenko
This looks really helpful for a problem I'm trying to solve. However, I get this error:
groovy.lang.ReadOnlyPropertyException: Cannot set readonly property: properties for class: UserForm on this line: form.properties = params I'm not sure how to fix this. Thanks
|
|
"properties" by default in groovy is a read-only property. Grails overrides this and makes it settable for domain classes only. If your object is not a domain object you will have to use the bindData() method of the controller classes.
On Mon, May 12, 2008 at 7:51 AM, MattyN <[hidden email]> wrote:
|
| Powered by Nabble | Edit this page |
