src/Form/EventListener/AddPhoneNumberFieldSubscriber.php line 22

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Harmonizely\Form\EventListener;
  4. use Harmonizely\Model\EventTypeInterface;
  5. use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType;
  6. use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Form\FormEvent;
  9. use Symfony\Component\Form\FormEvents;
  10. use Symfony\Component\Validator\Constraints\NotBlank;
  11. class AddPhoneNumberFieldSubscriber implements EventSubscriberInterface
  12. {
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [FormEvents::PRE_SUBMIT => 'preSubmit'];
  16.     }
  17.     public function preSubmit(FormEvent $event): void
  18.     {
  19.         $form $event->getForm();
  20.         /** @var EventTypeInterface $eventType */
  21.         $eventType $form->getParent()->get('eventType')->getData();
  22.         if ($eventType->isShowPhone()) {
  23.             $constraints = [];
  24.             $constraints[] = new PhoneNumber();
  25.             if ($eventType->isPhoneFieldRequired()) {
  26.                 $constraints[] = new NotBlank();
  27.             }
  28.             $form->add('phoneNumber'PhoneNumberType::class, [
  29.                 'constraints' => $constraints,
  30.             ]);
  31.         }
  32.     }
  33. }