symfony adding file image constraints to form

let’s say you want to constraint what people are uploading and you don’t want to use assert statements in the entity. you can do it in the formbuilder

use Symfony\Component\Validator\Constraints\File;
...
$form = $this->createFormBuilder($entity)
            ->add('logo', FileType::class,
                array(
                    'label' => false,
                    'attr' => array(
                        'accept' => "image/jpeg, image/png"
                    ),
                    'constraints' => [
                        new File([
                            'maxSize' => '2M',
                            'mimeTypes' => [
                                'image/jpeg',
                                'image/png',
                            ],
                            'mimeTypesMessage' => 'Please upload a JPG or PNG',
                        ])
                    ]
                )
            )
Like it.? Share it:
Tags:

Comments are closed.