/** * @param FormEvent $event */ public function onBind(FormEvent $event) { $collection = $event->getForm()->getData(); $data = $event->getData(); // looks like there is no way to remove other listeners $event->stopPropagation(); if (!$collection) { $collection = $data; } elseif (count($data) === 0) { $this->modelManager->collectionClear($collection); } else { // merge $data into $collection foreach ($collection as $entity) { if (!$this->modelManager->collectionHasElement($data, $entity)) { $this->modelManager->collectionRemoveElement($collection, $entity); } else { $this->modelManager->collectionRemoveElement($data, $entity); } } foreach ($data as $entity) { $this->modelManager->collectionAddElement($collection, $entity); } } $event->setData($collection); }