fix behaviour when phone is newly created

This commit is contained in:
Julien Fastré 2021-03-28 23:38:53 +02:00
parent 2d76294c26
commit 127803a94c
3 changed files with 14 additions and 1 deletions

View File

@ -107,4 +107,9 @@ class PersonPhone
{ {
$this->date = $date; $this->date = $date;
} }
public function isEmpty(): bool
{
return empty($this->getDescription()) && empty($this->getPhonenumber());
}
} }

View File

@ -30,6 +30,7 @@ use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper;
use Chill\PersonBundle\Form\Type\GenderType; use Chill\PersonBundle\Form\Type\GenderType;
use Chill\PersonBundle\Form\Type\PersonAltNameType; use Chill\PersonBundle\Form\Type\PersonAltNameType;
use Chill\PersonBundle\Form\Type\PersonPhoneType; use Chill\PersonBundle\Form\Type\PersonPhoneType;
use Chill\PersonBundle\Entity\PersonPhone;
use Chill\PersonBundle\Form\Type\Select2MaritalStatusType; use Chill\PersonBundle\Form\Type\Select2MaritalStatusType;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\DateType; use Symfony\Component\Form\Extension\Core\Type\DateType;
@ -120,6 +121,9 @@ class PersonType extends AbstractType
'allow_delete' => true, 'allow_delete' => true,
'by_reference' => false, 'by_reference' => false,
'label' => false, 'label' => false,
'delete_empty' => function(PersonPhone $pp = null) {
return NULL === $pp || $pp->isEmpty();
}
]); ]);
if ($this->config['email'] === 'visible') { if ($this->config['email'] === 'visible') {

View File

@ -39,10 +39,14 @@ class PersonPhoneType extends AbstractType
]); ]);
$builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) { $builder->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) {
if (NULL === $event->getData()) {
return;
}
$oldPersonPhone = $this->em->getUnitOfWork() $oldPersonPhone = $this->em->getUnitOfWork()
->getOriginalEntityData($event->getData()); ->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()); $type = $this->phonenumberHelper->getType($event->getData()->getPhonenumber());
$event->getData()->setType($type); $event->getData()->setType($type);
} }