Here's how I did it in my form template:
{{ form_errors(form.addresses) }} {% for address in form.addresses %} <div id="{{ 'address%sDivId'|format(loop.index) }}" class="userAddressItem"> <h5> Address #{{ loop.index }}</h5> {{ form_errors(address) }} {{ form_widget(address) }} </div> {% endfor %}
And I have a small jQuery-driven action bar that allows the user to add and remove addresses. This is a simple script adding a new div to a container with the correct HTML code. For HTML, I just used the same output as Symfony, but with an updated index. For example, this will be the output for street text of the AddressType form:
<input id="user_addresses_0_street" name="user[addresses][0][street]" ...>
Then the next Symfony index will be set to 1, so the newly added input field will look like this:
<input id="user_addresses_1_street" name="user[addresses][1][street]" ...>
Note. Three points are a substitute for required="required" maxlength="255" , but may vary depending on your needs.
You will need more HTML code than adding a whole new AddressType to the DOM, but this will give you a general idea.
Yours faithfully,
Matt
Matt
source share