How to use mimeType Assert with VichUploader?

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',
                        ]
                    ])
                ]
            ])

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 %}

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()
            ;
        }
    }
}

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();
       }
    }