8000 feature #8510 Added doc entry for delete_empty form option with calla… · symfony/symfony-docs@5e4c033 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5e4c033

Browse files
committed
feature #8510 Added doc entry for delete_empty form option with callable (hiddewie, javiereguiluz)
This PR was merged into the 3.4 branch. Discussion ---------- Added doc entry for delete_empty form option with callable Fix #8244. I hope the explanation is clear enough. Now it is just an addition to the existing text. The piece of code is taken from the tests (adapted slightly for simplicity). Commits ------- 0373a97 Processed xabbuh comments 3f1a814 Minor rewords 1722628 Added doc entry for delete_empty form option with callable
2 parents 1fe2c44 + 0373a97 commit 5e4c033

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

reference/forms/types/collection.rst

Lines changed: 24 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,28 @@ 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 executed
291+
for each value in the submitted collection. If the callable returns ``true``,
292+
the value is 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 compound form types, which
305+
may define complex conditions for considering them empty.
306+
307+
.. versionadded:: 3.4
308+
Support for using a callable for the ``delete_empty`` option was introduced
309+
in Symfony 3.4.
310+
289311
entry_options
290312
~~~~~~~~~~~~~
291313

0 commit comments

Comments
 (0)
0