src/Form/EventSubscriber/MaxFirmware/MaxFirmwareFileFieldSubscriber.php line 23

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Form\EventSubscriber\MaxFirmware;
  4. use App\Entity\MaxFirmware;
  5. use App\Validator\Constraint\FirmwareFileExtension;
  6. use App\Validator\Constraint\FirmwareFileRequiredOnCreate;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Form\Extension\Core\Type\FileType;
  9. use Symfony\Component\Form\FormEvent;
  10. use Symfony\Component\Form\FormEvents;
  11. use Symfony\Component\Validator\Constraints\File;
  12. class MaxFirmwareFileFieldSubscriber implements EventSubscriberInterface
  13. {
  14.     public static function getSubscribedEvents(): array
  15.     {
  16.         return [FormEvents::PRE_SET_DATA => 'onPreSetData'];
  17.     }
  18.     public function onPreSetData(FormEvent $event): void
  19.     {
  20.         $form $event->getForm();
  21.         $data $event->getData();
  22.         $isNew $data instanceof MaxFirmware && $data->getId() === null;
  23.         $form->add('firmwareFile'FileType::class, [
  24.             'required' => $isNew,
  25.             'label' => 'customStrings.form.firmware_management.label_fm_file',
  26.             'data_class' => null,
  27.             'mapped' => false,
  28.             'constraints' => [
  29.                 new File(['maxSize' => '10M']),
  30.                 new FirmwareFileRequiredOnCreate(),
  31.                 new FirmwareFileExtension(),
  32.             ],
  33.         ]);
  34.     }
  35. }