[英]symfony2 image type constraint validation
我知道上载文件的首选方式是通过书中解释的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()方法接受一个具有$ image属性的UploadedImage对象,以及一个具有File type属性的setter方法。 构造函数如下...
$uploadedImage = new UploadedImage();
$validator = $this->get('validator');
$errors = $validator->validate($uploadedImage);
var_dump($errors);
如果我上传的图片大于5M,则$ errors为空。 如果我不上传任何内容,也是一样。 我搜索了有关如何在文件上载中使用约束的教程,但没有成功,但还是在网络上搜索了堆栈溢出问题。 谁能解释我在做什么错?
根据您如何创建新的UploadedImage实体并尝试立即对其进行验证,我怀疑您没有正确处理表单数据。
逐步查看Symfony2文件上传 -它也概述了上传操作,但使用批注进行了验证。
您不必使用原则,因此可以忽略任何@ORM批注。 您只需要输出任何您需要输出的内容,即可完成请求。
另外,我不确定它的粘贴方式,但请检查validation.yml中的缩进级别(notFoundMessage,uploadErrorMessage等未正确缩进)
Frizer\AdminBundle\Entity\UploadedImage:
properties:
image:
- Image:
notFoundMessage: Image not uploaded
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.