Search code examples
phpfilesymfonymime-typesassert

Assert File not validating


I have followed the instructions on the documentation for how to handle file upload using Doctrine.

Image upload works fine but when I upload some other file type, it just lets the file upload even if I have properly set the annotation on the $file property like this:

/**
 * @Assert\File(
 *     maxSizeMessage = "L'image ne doit pas dépasser 5Mb.",
 *     maxSize = "5000k",
 *     mimeTypes = {"image/jpg", "image/jpeg", "image/gif", "image/png"},
 *     mimeTypesMessage = "Les images doivent être au format JPG, GIF ou PNG."
 * )
 */
public $file;

Solution

  • I just figured out that the problem was coming from the Product entity. I was adding multiple images to a Product form and I forgot to put the Valid assert to the $images property on the Product entity like so :

    /**
     * @ORM\ManyToMany(targetEntity="PS\StockBundle\Entity\Image", cascade={"persist"})
     * @Assert\Valid()
     */
    private $images;
    

    I wrote an article on my blog about this here https://web.archive.org/web/20141004165731/http://www.ennazk.com:80/validate-subforms-in-symfony2/#.Wdt9mBNSwnU

    Thanks.