src/Form/EventListener/RegistrationSubscriber.php line 21

Open in your IDE?
  1. <?php
  2. // src/Form/EventListener/RegistrationSubscriber.php
  3. namespace App\Form\EventListener;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  6. use Symfony\Component\Form\FormEvent;
  7. use Symfony\Component\Form\FormEvents;
  8. use Symfony\Component\Form\Extension\Core\Type\DateType;
  9. use App\Entity\User;
  10. class RegistrationSubscriber implements EventSubscriberInterface
  11. {
  12.     public static function getSubscribedEvents(): array
  13.     {
  14.         return [
  15.             FormEvents::SUBMIT   => 'onSubmit',
  16.         ];
  17.     }
  18.     public function onSubmit(FormEvent $event): void
  19.     {
  20.        
  21.         $entity $event->getData();
  22.         if($entity instanceof User) {
  23.             $entity->setRoles(['ROLE_ADMIN']);
  24.             $date = new \DateTime();
  25.             $entity->setCreatedAt($date);
  26.             $entity->setUpdatedAt($date);
  27.         }    
  28.     }
  29. }