How to use mimeType Assert with VichUploader?
Asked Answered
C

5

10

This assert is passing Symfony's form validation when uploading any file with VichUploaderBundle:

/**
 * @Vich\UploadableField(mapping="product_media", fileNameProperty="path")
 * @Assert\File(
 *     mimeTypes = {"image/jpeg", "image/gif", "image/png", "video/mp4", "video/quicktime", "video/avi"},
 *     mimeTypesMessage = "Wrong file type (jpg,gif,png,mp4,mov,avi)"
 * )
 * @var File $pathFile
 */
protected $pathFile;

I cannot see what the problem is with the assert. How can I validate file types with VichUploader?

Comte answered 1/12, 2015 at 11:33 Comment(0)
P
6

You can use validation callback to solve this issue.

/**
 * @ORM\Entity(repositoryClass="AppBundle\Entity\Repository\EntityRepository")
 * @ORM\Table(name="entity")
 * @Assert\Callback(methods={"validate"})
 * @Vich\Uploadable
 */
class Entity
{
    /**
     * @Assert\File(maxSize="10M")
     * @Vich\UploadableField(mapping="files", fileNameProperty="fileName")
     *
     * @var File $file
     */
    protected $file;

    /**
     * @ORM\Column(type="string", length=255, name="file_name", nullable=true)
     *
     * @var string $fileName
     */
    protected $fileName;

...

    /**
     * @param ExecutionContextInterface $context
     */
    public function validate(ExecutionContextInterface $context)
    {
        if (! in_array($this->file->getMimeType(), array(
            'image/jpeg',
            'image/gif',
            'image/png',
            'video/mp4',
            'video/quicktime',
            'video/avi',
        ))) {
            $context
                ->buildViolation('Wrong file type (jpg,gif,png,mp4,mov,avi)')
                ->atPath('fileName')
                ->addViolation()
            ;
        }
    }
}
Perished answered 15/12, 2015 at 5:1 Comment(3)
The validation silently fails on symfony 2.7Habitual
In newer versions of Symfony, the Callback annotation has to be added to the methods doc block rather than the class's: symfony.com/doc/current/reference/constraints/…Slocum
Note I needed to add the @Assert\Callback annotation for the validate() function for this to work. See: symfony.com/doc/current/reference/constraints/…Araminta
E
8

For Symfony 4.0 you'll need to import the Validator component

composer require validator

Now in your Entity class you can use the @Assert annotation.

// src/Entity/Author.php

// ...
use Symfony\Component\Validator\Constraints as Assert;

class Author
{
    /**
     * @Assert\NotBlank()
     */
    public $name;
}

You might need to add some configuration in your config/packages/framework.yaml file. Anyway, all this is perfectly explained on the official Symfony documentation.

http://symfony.com/doc/current/validation.html

To check the mime type you'll need to use the File constraint http://symfony.com/doc/current/reference/constraints/File.html

Here is a working exemple

/**
 * @ORM\Column(type="string", length=255)
 * @var string
 */
private $cvFilename;

/**
 * @Assert\File(
 *     maxSize = "2048k",
 *     mimeTypes = {"application/pdf", "application/x-pdf"},
 *     mimeTypesMessage = "Please upload a valid PDF"
 * )
 * @Vich\UploadableField(mapping="cv", fileNameProperty="cvFilename")
 * @var File
 */
private $cvFile;

Now it's true that there is a mime and size option inside the @Vich\UploadableField anotation as described here https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/usage.md#step-2-link-the-upload-mapping-to-an-entity but I couldn't get this to work.

The @Assert annotation will generate Forms errors, that you can retrieve them in Twig to give a feedback.

The key is to use : form_errors(candidature_form.cvFile)

here is a working example :

 {% set error_flag = form_errors(candidature_form.cvFile) %}

        <label class=" {% if error_flag %}has-error{% endif %}">
            Curriculum Vitae (PDF)
        </label>
        {{ form_widget(candidature_form.cvFile) }}
        {% if error_flag %}
            <div class="has-error">
                {{ form_errors(candidature_form.cvFile) }}
            </div>
        {% endif %}
Extrasensory answered 16/1, 2018 at 15:39 Comment(2)
Is it possible to i18n the messages set in annotations? If not, this solution is not so great...Pasteurism
I think it's ok. I haven't test it though symfony.com/doc/current/validation/translations.htmlExtrasensory
P
6

You can use validation callback to solve this issue.

/**
 * @ORM\Entity(repositoryClass="AppBundle\Entity\Repository\EntityRepository")
 * @ORM\Table(name="entity")
 * @Assert\Callback(methods={"validate"})
 * @Vich\Uploadable
 */
class Entity
{
    /**
     * @Assert\File(maxSize="10M")
     * @Vich\UploadableField(mapping="files", fileNameProperty="fileName")
     *
     * @var File $file
     */
    protected $file;

    /**
     * @ORM\Column(type="string", length=255, name="file_name", nullable=true)
     *
     * @var string $fileName
     */
    protected $fileName;

...

    /**
     * @param ExecutionContextInterface $context
     */
    public function validate(ExecutionContextInterface $context)
    {
        if (! in_array($this->file->getMimeType(), array(
            'image/jpeg',
            'image/gif',
            'image/png',
            'video/mp4',
            'video/quicktime',
            'video/avi',
        ))) {
            $context
                ->buildViolation('Wrong file type (jpg,gif,png,mp4,mov,avi)')
                ->atPath('fileName')
                ->addViolation()
            ;
        }
    }
}
Perished answered 15/12, 2015 at 5:1 Comment(3)
The validation silently fails on symfony 2.7Habitual
In newer versions of Symfony, the Callback annotation has to be added to the methods doc block rather than the class's: symfony.com/doc/current/reference/constraints/…Slocum
Note I needed to add the @Assert\Callback annotation for the validate() function for this to work. See: symfony.com/doc/current/reference/constraints/…Araminta
F
2

For Symfony 4.x, this solution not working. i don't know why assert or event validator never call ...

I've find this solution : Validation doesn't work on relation fields

         use Symfony\Component\Validator\Constraints\File;
        /* ... */
        ->add('ba_file', VichFileType::class, [
                'label' => 'Bon d\'adhésion (PDF file)',
                'required' => false,
                'constraints' => [
                    new File([
                        'maxSize' => '5M',
                        'mimeTypes' => [
                            'image/jpeg',
                            'image/gif',
                            'image/png',
                        ]
                    ])
                ]
            ])
Farias answered 17/1, 2019 at 16:5 Comment(1)
Have you been able to find out why this wasn't working as expected with @assert/Valid ?Moue
I
2

Symfony 6 - PHP 8:

Entity class:

#[Vich\UploadableField(mapping: 'avatars', fileNameProperty: 'avatarName')]
#[Assert\File(maxSize: '1024k', mimeTypes: ['image/jpeg', 'image/png'])]
private ?File $avatarFile = null;

FormType class:

 'attr' => [
   'accept' => 'image/jpeg, image/png',
 ],
 'constraints' => [
    new Assert\File([
       'maxSize' => '1024k',
       'mimeTypes' => ['image/jpeg', 'image/png'],
    ]),
 ],
Inexpressive answered 15/8, 2022 at 22:31 Comment(0)
K
1

For Symfony 3.0+, only 2 things need to be done:

  • Add use statement to import ExecutionContextInterface.

  • The callback annotation has to be added directly to the method/function instead of the class.

    use Symfony\Component\Validator\Context\ExecutionContextInterface;
    
    /**
    * @Assert\File(maxSize="2M")
    * @Vich\UploadableField(mapping="profile_image", fileNameProperty="avatar")
    * @var File
    */
    private $imageFile;
    
    /**
    * @ORM\Column(length=255, nullable=true)
    * @var string $avatar
    */
    protected $avatar;
    
    /**
    * @Assert\Callback
    * @param ExecutionContextInterface $context
    */
    public function validate(ExecutionContextInterface $context, $payload)
    {
       // do your own validation
       if (! in_array($this->imageFile->getMimeType(), array(
           'image/jpeg',
           'image/gif',
           'image/png'
    ))) {
        $context
            ->buildViolation('Wrong file type (only jpg,gif,png allowed)')
            ->atPath('imageFile')
            ->addViolation();
       }
    }
    
Kirimia answered 14/3, 2017 at 0:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.