44collection Field Type
55=====================
66
7- This field type is used to render a "collection" of some field or form. In
8- the easiest sense, it could be an array of ``text `` fields that populate
7+ This field type is used to render a "collection" of some field or form.
8+ In the easiest sense, it could be an array of ``text `` fields that populate
99an array ``emails `` field. In more complex examples, you can embed entire
10- forms, which is useful when creating forms that expose one-to-many relationships
11- (e.g. a product from where you can manage many related product photos).
10+ forms, which is useful when creating forms that expose one-to-many
11+ relationships (e.g. a product from where you can manage many related product
12+ photos).
1213
1314+-------------+-----------------------------------------------------------------------------+
1415| Rendered as | depends on the `type `_ option |
@@ -45,8 +46,8 @@ forms, which is useful when creating forms that expose one-to-many relationships
4546Basic Usage
4647-----------
4748
48- This type is used when you want to manage a collection of similar items in
49- a form. For example, suppose you have an ``emails `` field that corresponds
49+ This type is used when you want to manage a collection of similar items
50+ in a form. For example, suppose you have an ``emails `` field that corresponds
5051to an array of email addresses. In the form, you want to expose each email
5152address as its own input text box::
5253
@@ -104,8 +105,8 @@ A much more flexible method would look like this:
104105 <?php endforeach ?>
105106 </ul>
106107
107- In both cases, no input fields would render unless your ``emails `` data array
108- already contained some emails.
108+ In both cases, no input fields would render unless your ``emails `` data
109+ array already contained some emails.
109110
110111In this simple example, it's still impossible to add new addresses or remove
111112existing addresses. Adding new addresses is possible by using the `allow_add `_
@@ -141,7 +142,11 @@ will look like this:
141142
142143.. code-block :: html
143144
144- <input type =" email" id =" form_emails___name__" name =" form[emails][__name__]" value =" " />
145+ <input type =" email"
146+ id =" form_emails___name__"
147+ name =" form[emails][__name__]"
148+ value =" "
149+ />
145150
146151By replacing ``__name__ `` with some unique value (e.g. ``2 ``),
147152you can build and insert new HTML fields into your form.
@@ -160,7 +165,8 @@ you need is the JavaScript:
160165 {# ... #}
161166
162167 {# store the prototype on the data-prototype attribute #}
163- <ul id="email-fields-list" data-prototype="{{ form_widget(form.emails.vars.prototype)|e }}">
168+ <ul id="email-fields-list"
169+ data-prototype="{{ form_widget(form.emails.vars.prototype)|e }}">
164170 {% for emailField in form.emails %}
165171 <li>
166172 {{ form_errors(emailField) }}
@@ -203,10 +209,10 @@ you need is the JavaScript:
203209
204210 If you're rendering the entire collection at once, then the prototype
205211 is automatically available on the ``data-prototype `` attribute of the
206- element (e.g. ``div `` or ``table ``) that surrounds your collection. The
207- only difference is that the entire "form row" is rendered for you, meaning
208- you wouldn't have to wrap it in any container element as it was done
209- above.
212+ element (e.g. ``div `` or ``table ``) that surrounds your collection.
213+ The only difference is that the entire "form row" is rendered for you,
214+ meaning you wouldn't have to wrap it in any container element as it
215+ was done above.
210216
211217Field Options
212218-------------
@@ -222,8 +228,9 @@ items as well as the new item that was in the submitted data. See the above
222228example for more details.
223229
224230The `prototype `_ option can be used to help render a prototype item that
225- can be used - with JavaScript - to create new form items dynamically on the
226- client side. For more information, see the above example and :ref: `cookbook-form-collections-new-prototype `.
231+ can be used - with JavaScript - to create new form items dynamically on
232+ the client side. For more information, see the above example and
233+ :ref: `cookbook-form-collections-new-prototype `.
227234
228235.. caution ::
229236
@@ -249,11 +256,11 @@ For more information, see :ref:`cookbook-form-collections-remove`.
249256
250257 Be careful when using this option when you're embedding a collection
251258 of objects. In this case, if any embedded forms are removed, they *will *
252- correctly be missing from the final array of objects. However, depending on
253- your application logic, when one of those objects is removed, you may want
254- to delete it or at least remove its foreign key reference to the main object.
255- None of this is handled automatically. For more information, see
256- :ref: `cookbook-form-collections-remove `.
259+ correctly be missing from the final array of objects. However, depending
260+ on your application logic, when one of those objects is removed, you
261+ may want to delete it or at least remove its foreign key reference to
262+ the main object. None of this is handled automatically. For more
263+ information, see :ref: `cookbook-form-collections-remove `.
257264
258265options
259266~~~~~~~
@@ -262,8 +269,9 @@ options
262269
263270This is the array that's passed to the form type specified in the `type `_
264271option. For example, if you used the :doc: `choice </reference/forms/types/choice >`
265- type as your `type `_ option (e.g. for a collection of drop-down menus), then
266- you'd need to at least pass the ``choices `` option to the underlying type::
272+ type as your `type `_ option (e.g. for a collection of drop-down menus),
273+ then you'd need to at least pass the ``choices `` option to the underlying
274+ type::
267275
268276 $builder->add('favorite_cities', 'collection', array(
269277 'type' => 'choice',
@@ -283,13 +291,13 @@ prototype
283291**type **: ``boolean `` **default **: ``true ``
284292
285293This option is useful when using the `allow_add `_ option. If ``true `` (and
286- if `allow_add `_ is also ``true ``), a special "prototype" attribute will be
287- available so that you can render a "template" example on your page of what
288- a new element should look like. The ``name `` attribute given to this element
289- is ``__name__ ``. This allows you to add a "add another" button via JavaScript
290- which reads the prototype, replaces ``__name__ `` with some unique name or
291- number, and render it inside your form. When submitted, it will be added
292- to your underlying array due to the `allow_add `_ option.
294+ if `allow_add `_ is also ``true ``), a special "prototype" attribute will
295+ be available so that you can render a "template" example on your page of
296+ what a new element should look like. The ``name `` attribute given to this
297+ element is ``__name__ ``. This allows you to add a "add another" button via
298+ JavaScript which reads the prototype, replaces ``__name__ `` with some unique
299+ name or number and render it inside your form. When submitted, it will
300+ be added to your underlying array due to the `allow_add `_ option.
293301
294302The prototype field can be rendered via the ``prototype `` variable in the
295303collection field:
@@ -313,34 +321,35 @@ rendering your form, having the entire "form row" may be easier for you.
313321 form row is automatically available on the ``data-prototype `` attribute
314322 of the element (e.g. ``div `` or ``table ``) that surrounds your collection.
315323
316- For details on how to actually use this option, see the above example as well
317- as :ref: `cookbook-form-collections-new-prototype `.
324+ For details on how to actually use this option, see the above example as
325+ well as :ref: `cookbook-form-collections-new-prototype `.
318326
319327prototype_name
320328~~~~~~~~~~~~~~
321329
322330**type **: ``String `` **default **: ``__name__ ``
323331
324332If you have several collections in your form, or worse, nested collections
325- you may want to change the placeholder so that unrelated placeholders are not
326- replaced with the same value.
333+ you may want to change the placeholder so that unrelated placeholders are
334+ not replaced with the same value.
327335
328336type
329337~~~~
330338
331339**type **: ``string `` or :class: `Symfony\\ Component\\ Form\\ FormTypeInterface ` **required **
332340
333- This is the field type for each item in this collection (e.g. ``text ``, `` choice ``,
334- etc). For example, if you have an array of email addresses, you'd use the
335- :doc: `email </reference/forms/types/email >` type. If you want to embed
336- a collection of some other form, create a new instance of your form type
337- and pass it as this option.
341+ This is the field type for each item in this collection (e.g. ``text ``,
342+ `` choice ``, etc). For example, if you have an array of email addresses,
343+ you'd use the :doc: `email </reference/forms/types/email >` type. If you want
344+ to embed a collection of some other form, create a new instance of your
345+ form type and pass it as this option.
338346
339347Inherited Options
340348-----------------
341349
342- These options inherit from the :doc: `form </reference/forms/types/form >` type.
343- Not all options are listed here - only the most applicable to this type:
350+ These options inherit from the :doc: `form </reference/forms/types/form >`
351+ type. Not all options are listed here - only the most applicable to this
352+ type:
344353
345354.. _reference-form-types-by-reference :
346355
0 commit comments