diff --git a/src/Bundle/ChillPersonBundle/Entity/PersonPhone.php b/src/Bundle/ChillPersonBundle/Entity/PersonPhone.php index 1916404b9..4a2ffaf28 100644 --- a/src/Bundle/ChillPersonBundle/Entity/PersonPhone.php +++ b/src/Bundle/ChillPersonBundle/Entity/PersonPhone.php @@ -107,4 +107,9 @@ class PersonPhone { $this->date = $date; } + + public function isEmpty(): bool + { + return empty($this->getDescription()) && empty($this->getPhonenumber()); + } } diff --git a/src/Bundle/ChillPersonBundle/Form/PersonType.php b/src/Bundle/ChillPersonBundle/Form/PersonType.php index 0e4830f04..d87fe0196 100644 --- a/src/Bundle/ChillPersonBundle/Form/PersonType.php +++ b/src/Bundle/ChillPersonBundle/Form/PersonType.php @@ -30,6 +30,7 @@ use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper; use Chill\PersonBundle\Form\Type\GenderType; use Chill\PersonBundle\Form\Type\PersonAltNameType; use Chill\PersonBundle\Form\Type\PersonPhoneType; +use Chill\PersonBundle\Entity\PersonPhone; use Chill\PersonBundle\Form\Type\Select2MaritalStatusType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\DateType; @@ -120,6 +121,9 @@ class PersonType extends AbstractType 'allow_delete' => true, 'by_reference' => false, 'label' => false, + 'delete_empty' => function(PersonPhone $pp = null) { + return NULL === $pp || $pp->isEmpty(); + } ]); if ($this->config['email'] === 'visible') { diff --git a/src/Bundle/ChillPersonBundle/Form/Type/PersonPhoneType.php b/src/Bundle/ChillPersonBundle/Form/Type/PersonPhoneType.php index 63f4fbf4f..f06d5cd72 100644 --- a/src/Bundle/ChillPersonBundle/Form/Type/PersonPhoneType.php +++ b/src/Bundle/ChillPersonBundle/Form/Type/PersonPhoneType.php @@ -39,10 +39,14 @@ class PersonPhoneType extends AbstractType ]); $builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) { + if (NULL === $event->getData()) { + return; + } + $oldPersonPhone = $this->em->getUnitOfWork() ->getOriginalEntityData($event->getData()); - if ($oldPersonPhone['phonenumber'] !== $event->getForm()->getData()->getPhonenumber()) { + if ($oldPersonPhone['phonenumber'] ?? null !== $event->getForm()->getData()->getPhonenumber()) { $type = $this->phonenumberHelper->getType($event->getData()->getPhonenumber()); $event->getData()->setType($type); }