Skip to content

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

Merged
merged 3 commits into from
Oct 29, 2017
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions reference/forms/types/collection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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``,
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, of course! Edited.

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());
},
Copy link
Member

@yceruto yceruto Oct 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The users field suggests a collection of type User, right?

'delete_empty' => function (User $user = null) {
    return null === $user || empty($user->getFirstName());
},

Something like this would be good for you?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Support for using a callable [...]


entry_options
~~~~~~~~~~~~~

Expand Down