การตรวจสอบ Symfony2 ของ OneToMany ArrayCollection ของเอนทิตีรูปภาพ

ฉันกำลังประสบปัญหาในการตรวจสอบความถูกต้องของไฟล์ที่อัปโหลดใหม่

ฉันมีเอนทิตีผลิตภัณฑ์ของฉัน:

// 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;
}

เอนทิตีรูปภาพนั้นง่ายมาก โดยมีชื่อ ขนาด ประเภทไมม์

และฉันกำลังทำงานกับ Listener การอัปโหลดแบบกำหนดเอง ดังนั้นฉันจึงไม่ได้ใช้ form และ form->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 Assert ในฟิลด์อื่น (เช่นชื่อ) - ใช้งานได้ ฉันจะได้รับข้อผิดพลาด แต่ด้วย ArrayCollection - ไม่ใช่

ฉันกำลังทำอะไรผิดและไม่พบข้อมูลในอินเทอร์เน็ต

กูรูสามารถช่วยฉันได้ไหม?


person Ilia Shakitko    schedule 20.07.2013    source แหล่งที่มา


คำตอบ (1)


เพื่อตรวจสอบความถูกต้องของคอลเลกชัน คุณสามารถใช้ ทั้งหมด และ เครื่องมือตรวจสอบที่ถูกต้อง

Acme\DemoBundle\Entity\Product:
    properties:
        images:
            - Valid: ~
            - All:
                - NotNull: ~

Acme\DemoBundle\Entity\Image:
    properties:
        file:
            - Image:
                minWidth: 200
                maxWidth: 400
                minHeight: 200
                maxHeight: 400
person Alexey B.    schedule 21.07.2013
comment
ฉันได้เพิ่ม * @ORM\OneToMany(targetEntity=Image, mappedBy=product, cascade={persist}) * @Assert\All({ * @Assert\Image( * minWidth = 10, * maxWidth = 20, * minHeight = 10, * maxHeight = 20 * ) * }) ป้องกัน $images; ไม่มีอะไรเปลี่ยนแปลง และฉันไม่มีคุณสมบัติ $file ทำไมคุณถึงเพิ่มมันเข้าไป? - person Ilia Shakitko; 21.07.2013
comment
ตกลง. ฉันค้นพบมันแล้ว ฉันได้เพิ่มคุณสมบัติ 'ไฟล์' แล้ว ตั้งค่า UploadedFile หรือ File บนมัน และการตรวจสอบความถูกต้องก็ใช้งานได้ ขอบคุณ - person Ilia Shakitko; 21.07.2013