断言文件未验证

时间:2012-12-31 00:55:00

标签: php file symfony mime-types assert

我已遵循文档中有关如何使用Doctrine处理文件上载的说明。

图片上传工作正常,但是当我上传其他一些文件类型时,即使我在$ file属性上正确设置了这样的注释,它也只是让文件上传:

/**
 * @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;

2 个答案:

答案 0 :(得分:4)

我刚才发现问题来自Product实体。我正在向Product表单添加多个图像,我忘了将Valid assert放到Product实体上的$ images属性中,如下所示:

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

我在我的博客上写了一篇关于此问题的文章https://web.archive.org/web/20141004165731/http://www.ennazk.com:80/validate-subforms-in-symfony2/#.Wdt9mBNSwnU

感谢。

答案 1 :(得分:0)

{"image/jpg", "image/jpeg", "image/gif", "image/png"},有效json吗?因为对我来说这看起来不像是一个有效的json(json中的对象由key =>值对组成)。如果json解码失败,则可能无法应用整个断言。虽然我不太了解学说,所以我可能错了。

相关问题