Skip to content

Added doc entry for delete_empty form option with callable #8510

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

Merged
merged 3 commits into from
Oct 29, 2017
Merged
Changes from all commits
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
26 changes: 24 additions & 2 deletions reference/forms/types/collection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,10 @@ For more information, see :ref:`form-collections-remove`.
delete_empty
~~~~~~~~~~~~

**type**: ``Boolean`` **default**: ``false``
**type**: ``Boolean`` or ``callable`` **default**: ``false``

If you want to explicitly remove entirely empty collection entries from your
form you have to set this option to true. However, existing collection entries
form you have to set this option to ``true``. However, existing collection entries
will only be deleted if you have the allow_delete_ option enabled. Otherwise
the empty values will be kept.

Expand All @@ -286,6 +286,28 @@ the empty values will be kept.
Read about the :ref:`form's empty_data option <reference-form-option-empty-data>`
to learn why this is necessary.

A value is deleted from the collection only if the normalized value is ``null``.
However, you can also set the option value to a callable, which will be executed
for each value in the submitted collection. If the callable returns ``true``,
the value is removed from the collection. For example::

use Symfony\Component\Form\Extension\Core\Type\CollectionType;
// ...

$builder->add('users', CollectionType::class, array(
// ...
'delete_empty' => function (User $user = null) {
return null === $user || empty($user->getFirstName());
},
));

Using a callable is particularly useful in case of compound form types, which
may define complex conditions for considering them empty.

.. versionadded:: 3.4
Support for using a callable for the ``delete_empty`` option was introduced
in Symfony 3.4.

entry_options
~~~~~~~~~~~~~

Expand Down