【问题标题】:Symfony2 validation of OneToMany ArrayCollection of Image entitiesSymfony2 验证图像实体的 OneToMany ArrayCollection
【发布时间】:2013-07-20 18:35:23
【问题描述】:

我在验证新上传文件时遇到问题。

我有我的产品实体:

// src/Acme/DemoBundle/Entity/Product
...
/**
 * @ORM\OneToMany(targetEntity="Image", mappedBy="product", cascade={"persist"})
 * @Assert\Image(
 *     minWidth = 10,
 *     maxWidth = 20,
 *     minHeight = 10,
 *     maxHeight = 20
 * )
 */
protected $images;
...
public function __construct()
{
    $this->images= new \Doctrine\Common\Collections\ArrayCollection();
}
public function getImages(){
    return $this->images;
}

public function setImages($images){
    $this->images = $images;

    return $this;
}

图片实体很简单,有名字、大小、mimetype。

我正在研究一些自定义上传监听器,所以我没有使用表单和表单->isValid。我这样验证:

...
public function onUpload(PostPersistEvent $event)
{
        $em= $this->doctrine->getManager();
        $product = $this->doctrine->getRepository('Acme\DemoBundle\Entity\Product')->findOneById($customId);

        $image = new Image();
        $image->setProduct($product)
               ->setName($uploadInfo->name)
               ->setStoredName($uploadInfo->storedName)
               ->setUuid($uploadInfo->uuid)
               ->setSize($uploadInfo->size)
               ->setMimeType($uploadInfo->mimeType);

        $validator = Validation::createValidatorBuilder()
        ->enableAnnotationMapping()
        ->getValidator();

        $a = $product->getImages();
        $a->add($image);
        $product->setImages($a);

        $errors = $validator->validate($product);

我有一个错误:

{"message":"Expected argument of type string, object given","class":"Symfony\\Component\\Validator\\Exception\\UnexpectedTypeException","trace":[{"namespace":"","short_class":"","class":"","type":"","function":"","file":".../vendor\/symfony\/symfony\/src\/Symfony\/Component\/Validator\/Constraints\/FileValidator.php","line":98,"args":[]}

如果假设我在另一个字段(如名称)上进行 NotNull 注释断言 - 它有效,我可能会出错。但是使用 ArrayCollection - 不是。

我做错了,在互联网上找不到信息。

大师们能帮帮我吗?

【问题讨论】:

    标签: php image validation symfony doctrine-orm


    【解决方案1】:

    要验证集合,您可以使用 AllValid 验证器。

    Acme\DemoBundle\Entity\Product:
        properties:
            images:
                - Valid: ~
                - All:
                    - NotNull: ~
    
    Acme\DemoBundle\Entity\Image:
        properties:
            file:
                - Image:
                    minWidth: 200
                    maxWidth: 400
                    minHeight: 200
                    maxHeight: 400
    

    【讨论】:

    • 我已添加 * @ORM\OneToMany(targetEntity="Image", mappedBy="product", cascade={"persist"}) * @Assert\All({ * @Assert\Image (* minWidth = 10, * maxWidth = 20, * minHeight = 10, * maxHeight = 20 * ) * }) 受保护的 $images;没有改变。而且我没有 $file 属性,你为什么要添加它?
    • 好的。我发现了。我添加了属性“文件”。在其上设置 UploadedFile 或 File。验证是有效的。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    相关资源
    最近更新 更多