Jacek Kowalski
4 hours ago 6b9278a5d57091fce636dbb640b380cfb990db6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
declare(strict_types=1);
 
namespace App\Form;
 
use App\Service\CoverStorage;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints\Image;
use Symfony\Component\Validator\Constraints\NotNull;
 
/**
 * @extends AbstractType<array{cover: UploadedFile}>
 */
class CoverType extends AbstractType {
    private const int MAX_PIXELS = 20_000_000;
 
    public function buildForm(FormBuilderInterface $builder, array $options): void {
        $builder
            ->add('cover', FileType::class, [
                'label' => 'cover_file_header',
                'attr' => ['accept' => implode(',', array_keys(CoverStorage::EXTENSIONS))],
                'constraints' => [
                    new NotNull(),
                    new Image(
                        maxSize: '10M',
                        mimeTypes: array_keys(CoverStorage::EXTENSIONS),
                        maxPixels: self::MAX_PIXELS,
                        detectCorrupted: true,
                    ),
                ],
            ])
            ->add('save', SubmitType::class, ['label' => 'cover_action_upload']);
    }
}