Last updated by 3 years ago

Page: Editing multiple domain objects on the same page, Version:1

Imagine you have a page to edit a Book. It has on it a list of 3 author names. You want to add a "Add more authors" button to increase the list by another 3 entries, and all these authors will be created as domain objects on the fly when this single page is submitted. How?

Current solution

Nested domain objects on the same form - from Maurice Nicholson

In the HTML you make the input names appear as "nested" bean properties for the target bean. In your case the target bean is a User, which has an address property, so provide a value for "address1" on Address you use the "nested" property name "address.address1. Eg.

<input name="username" value="${user.username}" /> <!-- assuming these are properties of your User class -->
<input name="email" value="${user.email}" /> <!-- just to illustrate diff between nested and non-nested properties -->

<input name="address.address1" value="${user.address.address1}" /> <!-- these are properties of user.address" <input name="address.city" value="${user.address.city}" />

etc

Now in your controller, just:

def user = new User()
user.properties = params

Grails will automatically "descend" into nested properties and populate associated domain objects.

This works great for 1-1 mappings… 1-m is a different story!

Ideas for the future

Any takers?

http://omarello.com/2010/08/grails-one-to-many-dynamic-forms/