Skip to content

Commit 1722628

Browse files
committed
Added doc entry for delete_empty form option with callable
- Added User type hint - Added sentence about compound collection type use case - Added versionadded block
1 parent 7bb20ba commit 1722628

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

reference/forms/types/collection.rst

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,10 @@ For more information, see :ref:`form-collections-remove`.
270270
delete_empty
271271
~~~~~~~~~~~~
272272

273-
**type**: ``Boolean`` **default**: ``false``
273+
**type**: ``Boolean`` or ``callable`` **default**: ``false``
274274

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

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

289+
A value is deleted from the collection only if the normalized value is ``null``.
290+
However, you can also set the option value to a ``callable``, which will be called
291+
for each value in the submitted collection. The ``callable`` should return
292+
whether or not a value must be removed from the collection. For example::
293+
294+
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
295+
// ...
296+
297+
$builder->add('users', CollectionType::class, array(
298+
// ...
299+
'delete_empty' => function (User $user = null) {
300+
return null === $user || empty($user->getFirstName());
301+
},
302+
));
303+
304+
Using a ``callable`` is particularly useful in case of a compound form type
305+
which may have complex conditions for being empty.
306+
307+
.. versionadded:: 3.4
308+
Using a ``callable`` as an option value was introduced in Symfony 3.4.
309+
289310
entry_options
290311
~~~~~~~~~~~~~
291312

0 commit comments

Comments
 (0)