Skip to content

Improvements of the CollectionType Documentation #3338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions reference/forms/types/collection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ forms, which is useful when creating forms that expose one-to-many relationships
| | - `allow_delete`_ |
| | - `prototype`_ |
| | - `prototype_name`_ |
| | - `delete_empty`_ |
+-------------+-----------------------------------------------------------------------------+
| Inherited | - `label`_ |
| options | - `error_bubbling`_ |
| | - `error_mapping`_ |
| | - `by_reference`_ |
| | - `empty_data`_ |
| | - `required`_ |
| | - `mapped`_ |
+-------------+-----------------------------------------------------------------------------+
| Parent type | :doc:`form </reference/forms/types/form>` |
Expand All @@ -34,9 +36,9 @@ forms, which is useful when creating forms that expose one-to-many relationships

.. note::

If you are working with a collection of Doctrine entities, pay special
If you are working with a collection of Doctrine entities, pay special
attention to the `allow_add`_, `allow_delete`_ and `by_reference`_ options.
You can also see a complete example in the cookbook article
You can also see a complete example in the cookbook article
:doc:`/cookbook/form/form_collections`.

Basic Usage
Expand Down Expand Up @@ -333,6 +335,16 @@ If you have several collections in your form, or worse, nested collections
you may want to change the placeholder so that unrelated placeholders are not
replaced with the same value.

delete_empty
~~~~~~~~~~~~~~
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the tagline should be as long as the headline (remove 2 ~s)


**type**: ``Boolean`` **default**: ``false``

If you want to explicitly remove entirely empty collection entries from your
form you have to set this option to true. Existing collection entries will
however only be deleted if you have `allow_delete`_ option enabled, otherwise
the empty values will be kept.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rewrite this a bit:

However, existing collection entries will only be deleted if you have the allow_delete_ option enabled. Otherwise the empty values will be kept.


Inherited options
-----------------

Expand All @@ -357,3 +369,5 @@ error_bubbling
.. include:: /reference/forms/types/options/by_reference.rst.inc

.. include:: /reference/forms/types/options/empty_data.rst.inc

.. include:: /reference/forms/types/options/required.rst.inc
14 changes: 5 additions & 9 deletions reference/forms/types/options/empty_data.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@ choice is selected.

The true default value of this option depends on the field options:

* If ``compound`` is ``true`` and ``data_class`` is set, then ``new $data_class()``;
* If ``compound`` is ``true`` and no ``data_class`` is set, then ``array()``;
* If ``compound`` is ``false``, then ``null``.
* If ``required`` is ``true`` and ``data_class`` is set, then ``new $data_class()``;
* If ``required`` is ``true`` and no ``data_class`` is set, then ``array()``;
* If ``required`` is ``false``, then ``null``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure this is right, it looks to me (depending on the situation) that both required AND compound are important: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/Type/FormType.php#L136

Let me know what you see :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @weaverryan is right. So, this part should be. But I'm not sure if we need a table with eight (each possible combination of compound, required and data_class) rows for this. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think a table is the right choice here, just:

  • If data_class is set, and required is true, then null;
  • If data_class is set and required is false, then new $data_class();
  • If data_class is not set and compound is true, then array();
  • If data_class is not set and compound is false, then null.


.. tip::

The ``compound`` option is set to ``true`` when the field actually represents
a collection of fields (e.g. a form of fields).

For example, if you want the ``gender`` field to be set to ``null`` when no
value is selected, you can do it like this:
But you can customize this to your needs. For example, if you want the ``gender`` field to be
set to explicitly ``null`` when no value is selected, you can do it like this:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[...] to be explicitly set to null [...]


.. code-block:: php

Expand Down
7 changes: 6 additions & 1 deletion reference/forms/types/options/required.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ This is superficial and independent from validation. At best, if you let Symfony
guess your field type, then the value of this option will be guessed from
your validation information.

.. _`HTML5 required attribute`: http://diveintohtml5.info/forms.html
.. _`HTML5 required attribute`: http://diveintohtml5.info/forms.html
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add the note before this, such that external links are always the last elements in a file


.. note::

The required option does also affect the way how empty data of your form is
being handled. For further details check the :doc:` empty_data</reference/forms/types/options/empty_data.rst.inc>` option.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be indented by 4 spaces and you should wrap the lines around the 72th character and the space before empty_data should be placed after empty_data and you can't link to an included file, make sure the option is included on this page and just use:

`empty_data`_

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and, at last, always add a new line feed at the end of a file