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: 
I finally found a way to handle that with Event listeners.
This [discussion](https://stackoverflow.com/questions/9651095/description-of-symfony2-form-events) give the meaning of all FormEvents.
In this case, PRE\_BIND (replaced by PRE\_SUBMIT in 2.1 and later) will allow us to modify the data before it is bind to the Entity.

Looking at the implementation of [Form](https://github.com/symfony/Form/blob/master/Form.php) in Symfony source is the only source of information I found on how to use those Events. For PRE\_BIND, we see that the form data will be updated by the event data, so we can alter it with `$event->setData(...)`. The following snippet will loop through the data, unset all null values and set it back.

```
$builder->addEventListener(FormEvents::PRE_BIND, function(FormEvent $event){
    $data = $event->getData();
    if(isset($data["categories"])) {
        foreach($data as $key=>$value) {
            if(!isset($value) || $value == "")
                unset($data[$key]);
        }
        $event->setData($data);
});

```

Hope this can help others !
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>