<?php
declare(strict_types=1);
namespace App\Bundles\DiseaseCaseBundle\Form\Subscriber;
use App\Bundles\CatalogBundle\Entity\CatalogItem;
use App\Bundles\CatalogBundle\Enum\SystemCatalogAliasEnum;
use App\Platform\Form\Type\DatePickerType;
use App\Platform\Form\Type\Select2EntityType;
use DateTime;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Validator\Constraints as Assert;
class EditDiseaseCaseTypeSubscriber implements EventSubscriberInterface
{
public function __construct(private readonly UrlGeneratorInterface $router)
{
}
public static function getSubscribedEvents(): array
{
return [FormEvents::PRE_SUBMIT => 'preSubmit'];
}
public function preSubmit(FormEvent $event): void
{
if (!empty($event->getData()['hospitalization'])) {
$this->enableValidationForHospitalization($event->getForm());
}
}
private function enableValidationForHospitalization(FormInterface $form): void
{
$form
->add('hospitalizedAt', DatePickerType::class, [
'translation_domain' => 'DiseaseCaseBundle',
'label' => 'diseaseCase.hospitalizedAt',
'required' => true,
'max_date' => new DateTime(),
'constraints' => [
new Assert\NotBlank(allowNull: false),
],
'attr' => [
'placeholder' => 'form.placeholder.selectDate',
'autocomplete' => 'off',
'data-edit-disease-case-target' => 'hospitalizedAt',
],
])
->add('hospitalizationInstitution', Select2EntityType::class, [
'remote_path' => $this->router->generate(
'catalog-by-alias.items.autocomplete',
['systemAlias' => SystemCatalogAliasEnum::MEDICAL_INSTITUTION->value]
),
'class' => CatalogItem::class,
'primary_key' => 'id',
'text_property' => 'value',
'delay' => 250,
'language' => 'uk',
'translation_domain' => 'DiseaseCaseBundle',
'label' => 'diseaseCase.hospitalizationInstitution',
'placeholder' => 'form.placeholder.selectInstitution',
'required' => true,
'constraints' => [
new Assert\NotBlank(allowNull: false),
],
'attr' => [
'data-edit-disease-case-target' => 'hospitalizationInstitution',
],
]);
}
}