diff --git a/reference/forms/types/collection.rst b/reference/forms/types/collection.rst index 3d361c2801d..8b5d2cbb4dc 100644 --- a/reference/forms/types/collection.rst +++ b/reference/forms/types/collection.rst @@ -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. @@ -286,6 +286,28 @@ the empty values will be kept. Read about the :ref:`form's empty_data option ` 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 ~~~~~~~~~~~~~