src/Form/UploadFormType.php line 12

  1. <?php
  2. namespace App\Form;
  3. use App\Entity\ImageMetadata;
  4. use Symfony\Component\Form\AbstractType;
  5. use Symfony\Component\Form\Extension\Core\Type\FileType;
  6. use Symfony\Component\Form\FormBuilderInterface;
  7. use Symfony\Component\OptionsResolver\OptionsResolver;
  8. use Symfony\Component\Validator\Constraints\File;
  9. class UploadFormType extends AbstractType
  10. {
  11.     public function buildForm(FormBuilderInterface $builder, array $options)
  12.     {
  13.         $builder
  14.             // ...
  15.             ->add('media'FileType::class, [
  16.                 'label' => 'Media',
  17.                 // unmapped means that this field is not associated to any entity property
  18.                 'mapped' => false,
  19.                 // make it optional so you don't have to re-upload the PDF file
  20.                 // every time you edit the Product details
  21.                 'required' => false,
  22.                 // unmapped fields can't define their validation using annotations
  23.                 // in the associated entity, so you can use the PHP constraint classes
  24.                 //https://www.iana.org/assignments/media-types/media-types.xhtml -- see mimiTypes
  25.                 'constraints' => [
  26.                     new File([
  27.                         'maxSize' => '10240k',
  28.                         'mimeTypes' => [
  29.                             'application/pdf',
  30.                             'application/x-pdf',
  31.                             'image/png',
  32.                             'image/*'
  33.                         ],
  34.                         'mimeTypesMessage' => 'Please upload a valid PDF document',
  35.                     ])
  36.                 ],
  37.             ])
  38.             // ...
  39.         ;
  40.     }
  41.     public function configureOptions(OptionsResolver $resolver): void
  42.     {
  43.         $resolver->setDefaults([
  44.             'data_class' => ImageMetadata::class,
  45.             'method' =>'POST',
  46.             'csrf_token_id' => 'login_form_csrf' // <- explicit variable
  47.         ]);
  48.     }
  49. }