I have a template with a table of input box were each row represents a model object.
I can’t figure out what would be the best way to map this in my controller.
#{form id:'targetsForm', method:'POST', action:@createTargets()}
<table id="targetsTable">
<tr>
<th>Name</th>
<th>January</th>
<th>February</th>
<th>March</th>
</tr>
<tbody>
<tr>
<td class="center" id="object2">Object1</td>
<td class="center"><input id="01-jan" type="number" name="01-jan" size="20" value="" class="target-input" /></td>
<td class="center"><input id="01-feb" type="number" name="01-feb" size="20" value="" class="target-input" /></td>
<td class="center"><input id="01-mar" type="number" name="01-mar" size="20" value="" class="target-input" /></td>
</tr>
<tr>
<td class="center" id="object2">Object2</td>
<td class="center"><input id="02-jan" type="number" name="02-jan" size="20" value="" class="target-input" /></td>
<td class="center"><input id="02-feb" type="number" name="02-feb" size="20" value="" class="target-input" /></td>
<td class="center"><input id="02-mar" type="number" name="02-mar" size="20" value="" class="target-input" /></td>
</tr>
</tbody>
</table>
#{/form}
How could I map object 1 and object 2 in the routes and controller, know that there might be n objects in the form?
In your template, you could do something like this:
And then in your controller, your createTargets method would look like this:
You don’t need to do anything special to map the fields from the template to the controller. Play will map the names (i.e. january, february, march) to the name of the parameters in the controller.
Instead of having individual months as parameters into the action method, you could create an object like this:
And then the template would look like this:
Then the controller would be simply this: