src/Form/EventListener/AddLocationLabelFieldSubscriber.php line 24

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Harmonizely\Form\EventListener;
  4. use Harmonizely\Model\EventTypeInterface;
  5. use Symfony\Component\Form\Extension\Core\Type\TextType;
  6. use Symfony\Component\Form\FormEvent;
  7. use Symfony\Component\Form\FormEvents;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Validator\Constraints\Length;
  10. use Symfony\Component\Validator\Constraints\NotBlank;
  11. class AddLocationLabelFieldSubscriber implements EventSubscriberInterface
  12. {
  13.     public static function getSubscribedEvents(): array
  14.     {
  15.         return [
  16.             FormEvents::PRE_SUBMIT => 'preSubmit',
  17.         ];
  18.     }
  19.     public function preSubmit(FormEvent $event): void
  20.     {
  21.         $data $event->getData();
  22.         if (empty($data) || !array_key_exists('locationType'$data)) {
  23.             return;
  24.         }
  25.         if (EventTypeInterface::LOCATION_TYPE_REQUEST === $data['locationType']) {
  26.             $form $event->getForm();
  27.             $form->add('locationLabel'TextType::class, [
  28.                 'constraints' => [
  29.                     new NotBlank(),
  30.                     new Length([
  31.                         'max' => 255,
  32.                     ]),
  33.                 ],
  34.             ]);
  35.         }
  36.     }
  37. }