Search code examples
formsgrailsgrails-plugin

how to get several same object values in a Grails form


let's take a simpe domain class:

class Person { String aname }

A gsp form to let the user input a person is easy:

<g:form ...>
...
  someone:<input name="aname">
...
</g:form>

... and back in the controller, to get the values, I can just write

def p = new Person(params)

Now, I'd like to let the user input the data for two persons (let's say, two parents) in the same form. How to write this ? I just can't give the same name for the two input fields, but if I don't keep the original property name ("aname"), back in the controller I will have to handle by hand the bindings between the name of the property, and the form input names:

<g:form ...>
...
  father:<input name="aname1">
  mother:<input name="aname2">
...
</g:form>

then, in controller

def p1 = new Person(); p1.aname = params.aname1
def p2 = new Person(); p2.aname = params.aname2

Is there a way to keep the automatic binding facility even if there is several objects of same types given in a form ?


Solution

  • Try to use this way:

    <g:form ...>
    ...
      father:<input name="father.aname">
      mother:<input name="mother.aname">
    ...
    </g:form>
    

    And controller:

    def p1 = new Person(params.father); 
    def p2 = new Person(params.mother);