src/Bundles/DiseaseCaseBundle/Form/Subscriber/EditDiseaseCaseTypeSubscriber.php line 30

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Bundles\DiseaseCaseBundle\Form\Subscriber;
  4. use App\Bundles\CatalogBundle\Entity\CatalogItem;
  5. use App\Bundles\CatalogBundle\Enum\SystemCatalogAliasEnum;
  6. use App\Platform\Form\Type\DatePickerType;
  7. use App\Platform\Form\Type\Select2EntityType;
  8. use DateTime;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Form\FormEvent;
  11. use Symfony\Component\Form\FormEvents;
  12. use Symfony\Component\Form\FormInterface;
  13. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  14. use Symfony\Component\Validator\Constraints as Assert;
  15. class EditDiseaseCaseTypeSubscriber implements EventSubscriberInterface
  16. {
  17.     public function __construct(private readonly UrlGeneratorInterface $router)
  18.     {
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [FormEvents::PRE_SUBMIT => 'preSubmit'];
  23.     }
  24.     public function preSubmit(FormEvent $event): void
  25.     {
  26.         if (!empty($event->getData()['hospitalization'])) {
  27.             $this->enableValidationForHospitalization($event->getForm());
  28.         }
  29.     }
  30.     private function enableValidationForHospitalization(FormInterface $form): void
  31.     {
  32.         $form
  33.             ->add('hospitalizedAt'DatePickerType::class, [
  34.                 'translation_domain' => 'DiseaseCaseBundle',
  35.                 'label'              => 'diseaseCase.hospitalizedAt',
  36.                 'required'           => true,
  37.                 'max_date'           => new DateTime(),
  38.                 'constraints'        => [
  39.                     new Assert\NotBlank(allowNullfalse),
  40.                 ],
  41.                 'attr'               => [
  42.                     'placeholder'                   => 'form.placeholder.selectDate',
  43.                     'autocomplete'                  => 'off',
  44.                     'data-edit-disease-case-target' => 'hospitalizedAt',
  45.                 ],
  46.             ])
  47.             ->add('hospitalizationInstitution'Select2EntityType::class, [
  48.                 'remote_path'        => $this->router->generate(
  49.                     'catalog-by-alias.items.autocomplete',
  50.                     ['systemAlias' => SystemCatalogAliasEnum::MEDICAL_INSTITUTION->value]
  51.                 ),
  52.                 'class'              => CatalogItem::class,
  53.                 'primary_key'        => 'id',
  54.                 'text_property'      => 'value',
  55.                 'delay'              => 250,
  56.                 'language'           => 'uk',
  57.                 'translation_domain' => 'DiseaseCaseBundle',
  58.                 'label'              => 'diseaseCase.hospitalizationInstitution',
  59.                 'placeholder'        => 'form.placeholder.selectInstitution',
  60.                 'required'           => true,
  61.                 'constraints'        => [
  62.                     new Assert\NotBlank(allowNullfalse),
  63.                 ],
  64.                 'attr'               => [
  65.                     'data-edit-disease-case-target' => 'hospitalizationInstitution',
  66.                 ],
  67.             ]);
  68.     }
  69. }