how to bind list of ids to command object in grails?

grails controllers
grails 2.3 documentation
grails 2.5 6
grails 4 guide
grails 1.3 7 documentation
grails 3.3 10 documentation
grails 2.2 4 documentation
grails url builder

Let's say when you submit a form it sends a list of ids.

<form action="/process">
<input type="hidden" name="ids" value="4, 6, 10, 14, 20, 56" >
<input type="submit" value="Submit">
</form>

At the controller side

def process(EmailCommand cmd){

   //now iterating over registrations after data binding
   cmd.ids.each {

    }
}


//Command Object
class EmailCommand {
  List<Registration> ids
}

I want to bind all the ids passed to controller to the ids list in EmailCommand command object. How can i achieve it? I appreciate any help! Thanks!

It would be something like

<form action="/process">
    <input type="hidden" name="ids[0].id" value="4" >
    <input type="hidden" name="ids[1].id" value="6" >
    <input type="hidden" name="ids[2].id" value="10" >
    <input type="hidden" name="ids[3].id" value="14" >
    <input type="hidden" name="ids[4].id" value="20" >
    <input type="hidden" name="ids[5].id" value="56" >
    <input type="submit" value="Submit">
</form>

Or if you want something more dynamic :

<form action="/process">
    <g:each in="[4, 6, 10, 14, 20, 56]" var="id" status="i">
        <input type="hidden" name="ids[${i}]" value="${id}" >
    </g:each>
    <input type="submit" value="Submit">
</form>

Using Command Objects To Handle Form Data, A command object is a class that is used in conjunction with data binding, run the application and navigate to http://localhost:8080/player/ to see a list of players​, def update(PlayerInfo info) { Player player = Player.get(params.id) if (player  includesExcludes - (Optional) A map with 'include' and/or 'exclude' lists containing the names of properties to either include or exclude. prefix - (Optional) A string representing a prefix to use to filter parameters. The method will automatically append a '.' when matching the prefix to parameters,

I could only get it to work after changing the command object to

class EmailCommand{

    List<Registration> ids=  ListUtils.lazyList([], { new Registration() } as Factory )

}

and view to the following as bassmartin suggested.

<g:hiddenField name="ids[0].id" value="1"></g:hiddenField>
<g:hiddenField name="ids[1].id" value="2"></g:hiddenField>
<g:hiddenField name="ids[2].id" value="3"></g:hiddenField>
<g:hiddenField name="ids[3].id" value="4"></g:hiddenField>
<g:hiddenField name="ids[4].id" value="5"></g:hiddenField>


<g:submitButton name="submit" value="submit"></g:submitButton>

I am wondering why empty list in command object doesn't work. Is this limitation of grails version 2.2?

The Grails Framework 4.0.3, binds request parameters to a target object bindData(target, params) // exclude includesExcludes - (Optional) A map with 'include' and/or 'exclude' lists  Grails binds the parameters in the command object and calls validate() before the controller’s save action starts. 2 If the command object is valid, it tries to save the new student with the help of a service (a collaborator).

You have 2 options here:

Straight forward -> trick with the split comma-separated String in the "setter":

class EmailCommand {
  List<Registration> ids
  void setIds( String val ){ ids = Registration.getAll( val.split( ', ' ) ) }
}

Correct -> use form params for that:

<form action="/process">

<g:each in="[4, 6, 10, 14, 20, 56]" var="id">
  <input type="hidden" name="ids" value="${id}" >
</g:each>
<input type="submit" value="Submit">
</form>

and let grails do the binding.

Grails: Data binding with Lists, Grails: Data binding with Lists. Some days I wanted to bind multiple values from a GSP form to a List inside a command object. phones[0].number:​9495551212, phones[0].new:true, phones[0].deleted:false, phones[0].id:] target - The target object to bind to. params - A Map of source parameters, often the params object when used in a controller. includesExcludes - (Optional) A map with 'include' and/or 'exclude' lists containing the names of properties to either include or exclude. prefix - (Optional) A string representing a prefix to use to filter parameters. The method will automatically append a '.' when matching the prefix to parameters, so you can use 'author' to filter for parameters such as 'author.name'.

8.1 Controllers - 8.1.10 Command Objects, Grails controllers support the concept of command objects. If the command object's type is that of a domain class and there is an id request Nested parameter names may be used to bind down the object graph in an intuitive way. class AuthorCommand {; String fullName; List<BookCommand> books; }  Here, player is a command object, because it is an argument to the action. Behind the scenes, Grails will recognize this and rewrite your action to fetch existing records (if necessary), bind input data to the command object, perform dependency injection, and validate the object.

Grails 3.1.1, Grails 3.1.1 - command object data binding does not play well with commandIs: cmd] as JSON) } } class SomeCommandObject { Long id  max - The maximum number to list. offset - The offset from the first result to list from. order - How to order the list, either "desc" or "asc". sort - The property name to sort by. ignoreCase - Whether to ignore the case when sorting. Default is true. fetch - The fetch policy for the object's associations as a Map.

Data Binding Threats and Security with Grails, def save() { def b = Book.get(params.id) b.properties = params b.save() } a malicious binding of data and domain class, pre-defining a list of Data Validation using Command Object for data binding can also be used instead  Grails: Data binding with Lists Some days ago I had a bit of trouble with the data binding utilities of Grails. I wanted to bind multiple values from a GSP form to a List inside a command object.

Comments
  • i tried the second approach but it doesnt seem to work. I created 6 hidden fields each with the same name "ids" and value 1 interger per field. When i printed cmd.ids after binding it shows empty list []. Please note i am using grails 2.2
  • how are you binding?