【发布时间】:2014-03-05 18:08:07
【问题描述】:
我知道上传文件的首选方式是通过书中解释的 Doctrine,但我需要将图像数据保存在 xml 文件中,我也喜欢约束验证器。无需为它们编写太多代码。无论如何,我被困在图像约束上,我不知道我是否以正确的方式做所有事情。
我的 validator.yml 如下...
Frizer\AdminBundle\Entity\UploadedImage:
properties:
image:
- Image:
notFoundMessage: Image not uploaded
uploadErrorMessage: The image could not upload. Try again
uploadIniSizeErrorMessage: Image has to have more that 5M
mimeTypes: [image/jpeg, image/jpg, image/png]
mimeTypesMessage: Image has to be either jpg, jpeg or png format
minWidth: 500
minHeight: 500
minWidthMessage: Image has to be 500/500 px
minHeightMessage: Image has to be 500/500px
maxSize: 5M
maxSizeMessage: Image has to have more than 5M
validate() 方法接受一个 UploadedImage 对象,该对象具有 $image 属性和具有 File 类型属性的该属性的 setter 方法。构造函数如下...
$uploadedImage = new UploadedImage();
$validator = $this->get('validator');
$errors = $validator->validate($uploadedImage);
var_dump($errors);
如果我上传大于 5M 的图片,$errors 为空。如果我不上传任何东西,同样的事情。我通过有关如何在文件上传中使用约束的教程搜索了网络和堆栈溢出,但没有成功。谁能解释我做错了什么?
【问题讨论】:
标签: php validation symfony