src/Form/EventListener/Global/AddEmailFieldSubscriber.php line 28

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Form\EventListener\Global;
  4. use App\Entity\Security\Customer;
  5. use App\Form\FormHelper;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  8. use Symfony\Component\Form\FormEvent;
  9. use Symfony\Component\Form\FormEvents;
  10. class AddEmailFieldSubscriber extends FormHelper implements EventSubscriberInterface
  11. {
  12.     public function __construct(
  13.         private string $label 'app.global.fields.email',
  14.         private bool $required true,
  15.         private string $placeholder self::PLACEHOLDER_EMAIL,
  16.     ) {
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [FormEvents::PRE_SET_DATA => 'preSetData'];
  21.     }
  22.     public function preSetData(FormEvent $event): void
  23.     {
  24.         $data $event->getData();
  25.         if (null !== $data?->getId() && true === $data instanceof Customer) {
  26.             return;
  27.         }
  28.         $event->getForm()->add('email'EmailType::class, $this->getTextParameters($this->label$this->placeholder$this->required, [
  29.             'attr' => ['class' => 'email'],
  30.         ]));
  31.     }
  32. }