<?php
declare(strict_types=1);
namespace Harmonizely\Form\EventListener;
use Harmonizely\Model\EventTypeInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
class AddLocationLabelFieldSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
FormEvents::PRE_SUBMIT => 'preSubmit',
];
}
public function preSubmit(FormEvent $event): void
{
$data = $event->getData();
if (empty($data) || !array_key_exists('locationType', $data)) {
return;
}
if (EventTypeInterface::LOCATION_TYPE_REQUEST === $data['locationType']) {
$form = $event->getForm();
$form->add('locationLabel', TextType::class, [
'constraints' => [
new NotBlank(),
new Length([
'max' => 255,
]),
],
]);
}
}
}