Reputation: 3698
I have the following code in my Grails controller...
def teams = params.list('team')
And in the post being submitted I see in Chrome dev tools network tab...
team:Yankees mets
Two values separated by a space. On the server side this is read as a string, how can I pass the data in so it is read as an array (with "Yankees" being the first element and "mets" the second)?
I tried separating the values by comma instead but that didn't work either.
Upvotes: 0
Views: 2016
Reputation: 24776
It's important to understand what params.list()
expects you to submit to it and how that differs from other ways of sending in array like data.
params.list()
expects that you submit the same parameter multiple times either via GET
or POST
an example of such a form would be:
...
<input type="text" name="team" value="One" />
<input type="text" name="team" value="Two" />
...
Which would result in the following being posted as form data:
team=One
team=Two
The above example as a querystring (used in a GET
) would look like:
?team=One&team=Two
As you can see from the above params.list()
expects you to submit your data in a very specific way so it understands how to parse it into a collection (e.g. list
).
If, however, you wish to use a single field and separate your data using some token (such as a comma) you could parse the value of the single parameter into a list using either tokenize()
or split()
. For example:
List teams = params?.team?.tokenize(',')
You'll have to be careful about handling empty/null values. Also the above example expects that param.team
is going to be a String
(important to note if your are sending a comma separated list of numeric values and only one value is present).
Upvotes: 2