From 1722628d04b818982d5b4db148211daf445663ff Mon Sep 17 00:00:00 2001 From: Hidde Wieringa Date: Thu, 12 Oct 2017 19:45:14 +0200 Subject: [PATCH 1/3] 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 --- reference/forms/types/collection.rst | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/reference/forms/types/collection.rst b/reference/forms/types/collection.rst index 3d361c2801d..056717272da 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,27 @@ 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 called +for each value in the submitted collection. The ``callable`` should return +whether or not a value must be 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 a compound form type +which may have complex conditions for being empty. + +.. versionadded:: 3.4 + Using a ``callable`` as an option value was introduced in Symfony 3.4. + entry_options ~~~~~~~~~~~~~ From 3f1a8145e27059cf2f4a5c53d4b414470a7426e6 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 17 Oct 2017 17:41:31 +0200 Subject: [PATCH 2/3] Minor rewords --- reference/forms/types/collection.rst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/reference/forms/types/collection.rst b/reference/forms/types/collection.rst index 056717272da..eda70eded05 100644 --- a/reference/forms/types/collection.rst +++ b/reference/forms/types/collection.rst @@ -287,9 +287,9 @@ the empty values will be kept. 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 called -for each value in the submitted collection. The ``callable`` should return -whether or not a value must be removed from the collection. For example:: +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 ``false``, +the value is removed from the collection. For example:: use Symfony\Component\Form\Extension\Core\Type\CollectionType; // ... @@ -301,11 +301,11 @@ whether or not a value must be removed from the collection. For example:: }, )); -Using a ``callable`` is particularly useful in case of a compound form type -which may have complex conditions for being empty. +Using a callable is particularly useful in case of compound form types, which +may define complex conditions for considering them empty. .. versionadded:: 3.4 - Using a ``callable`` as an option value was introduced in Symfony 3.4. + Using a callable for the ``delete_empty`` option was introduced in Symfony 3.4. entry_options ~~~~~~~~~~~~~ From 0373a97bd638520aa928576f39f006bbf5a1e02d Mon Sep 17 00:00:00 2001 From: Hidde Wieringa Date: Sat, 28 Oct 2017 14:16:26 +0200 Subject: [PATCH 3/3] Processed xabbuh comments --- reference/forms/types/collection.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/reference/forms/types/collection.rst b/reference/forms/types/collection.rst index eda70eded05..8b5d2cbb4dc 100644 --- a/reference/forms/types/collection.rst +++ b/reference/forms/types/collection.rst @@ -288,7 +288,7 @@ the empty values will be kept. 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 ``false``, +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; @@ -305,7 +305,8 @@ Using a callable is particularly useful in case of compound form types, which may define complex conditions for considering them empty. .. versionadded:: 3.4 - Using a callable for the ``delete_empty`` option was introduced in Symfony 3.4. + Support for using a callable for the ``delete_empty`` option was introduced + in Symfony 3.4. entry_options ~~~~~~~~~~~~~