@@ -270,10 +270,10 @@ For more information, see :ref:`form-collections-remove`.
270
270
delete_empty
271
271
~~~~~~~~~~~~
272
272
273
- **type **: ``Boolean `` **default **: ``false ``
273
+ **type **: ``Boolean `` or `` callable `` **default **: ``false ``
274
274
275
275
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
277
277
will only be deleted if you have the allow_delete _ option enabled. Otherwise
278
278
the empty values will be kept.
279
279
@@ -286,6 +286,27 @@ the empty values will be kept.
286
286
Read about the :ref: `form's empty_data option <reference-form-option-empty-data >`
287
287
to learn why this is necessary.
288
288
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
+
289
310
entry_options
290
311
~~~~~~~~~~~~~
291
312
0 commit comments