Question: I'm trying to implement a ManyToMany relation in a form between 2 entities (say, Product and Category to make simpe) and use the method described in the docs with prototype and javascript (<http://symfony.com/doc/current/cookbook/form/form_collections.html>).

Here is the line from ProductType that create the category collection :

```
$builder->add('categories', 'collection', array(
                   'type' => 'entity',
                   'options' => array(
                        'class' => 'AppBundle:Category',
                        'property'=>'name',
                        'empty_value' => 'Select a category',
                        'required' => false),
                   'allow_add' => true,
                   'allow_delete' => true,
              ));

```

When I had a new item, a new select appear set to the empty value 'Select a category'. The problem is that if I don't change the empty value, it is sent to the server and after a $form->bind() my Product object get some null values in the $category ArrayCollection.

I first though to test the value in the setter in Product entity, and add 'by\_reference'=>false in the ProductType, but in this case I get an exception stating that null is not an instance of Category.

How can I make sure the empty values are ignored ?

Answer: 
Citing the [documentation](http://symfony.com/doc/current/reference/forms/types/collection.html#delete-empty) on 'delete\_empty':

> 
> If you want to explicitly remove entirely empty collection entries from your form you have to set this option to true
> 
> 
> 

```
$builder->add('categories', 'collection', array(
               'type' => 'entity',
               'options' => array(
                    'class' => 'AppBundle:Category',
                    'property'=>'name',
                    'empty_value' => 'Select a category'),
               'allow_add' => true,
               'allow_delete' => true,
               'delete_empty' => true
          ));

```

Since you use embedded forms, you could run in some issues such as `Warning: spl_object_hash() expects parameter 1 to be object, null given` when passing empty collections.

Removing `required=>false` as explained on this [answer](https://stackoverflow.com/questions/19474872/symfony2-form-collection-allow-add-and-allow-delete-null-error-silex) did not work for me.

A similar issue is referenced [here](https://github.com/symfony/symfony/issues/9375) on github and resolved by the PR [9773](https://github.com/symfony/symfony/pull/9773)
Since Symfony 3.4 you can pass a closure to `delete_empty`:

```php
$builder
    ->add('authors', CollectionType::class, [
        'delete_empty' => function ($author) {
            return empty($author['firstName']);
        },
    ]);

```

<https://github.com/symfony/symfony/commit/c0d99d13c023f9a5c87338581c2a4a674b78f85f>