-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Added doc entry for delete_empty form option with callable #8510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <reference-form-option-empty-data>` | ||
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 ``false``, | ||
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()); | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Something like this would be good for you? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! I've updated the code example. |
||
)); | ||
|
||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Support for using a callable [...] |
||
|
||
entry_options | ||
~~~~~~~~~~~~~ | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks wrong to me. The item is removed if the callable returns
true
, isn't?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, of course! Edited.