<?php
declare(strict_types=1);
namespace App\Form\EventSubscriber\MaxFirmware;
use App\Entity\MaxFirmware;
use App\Validator\Constraint\FirmwareFileExtension;
use App\Validator\Constraint\FirmwareFileRequiredOnCreate;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Validator\Constraints\File;
class MaxFirmwareFileFieldSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [FormEvents::PRE_SET_DATA => 'onPreSetData'];
}
public function onPreSetData(FormEvent $event): void
{
$form = $event->getForm();
$data = $event->getData();
$isNew = $data instanceof MaxFirmware && $data->getId() === null;
$form->add('firmwareFile', FileType::class, [
'required' => $isNew,
'label' => 'customStrings.form.firmware_management.label_fm_file',
'data_class' => null,
'mapped' => false,
'constraints' => [
new File(['maxSize' => '10M']),
new FirmwareFileRequiredOnCreate(),
new FirmwareFileExtension(),
],
]);
}
}