Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2 validation of OneToMany ArrayCollection of Image entities
    primarykey
    data
    text
    <p>I am facing a problem with validation of new uploaded file.</p> <p>I have my Product entity:</p> <pre><code>// 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-&gt;images= new \Doctrine\Common\Collections\ArrayCollection(); } public function getImages(){ return $this-&gt;images; } public function setImages($images){ $this-&gt;images = $images; return $this; } </code></pre> <p>Image entity is a very simple, with name, size, mimetype.</p> <p>And I have working on some custom upload listener, so I am not using form and form->isValid. I validate like this:</p> <pre><code>... public function onUpload(PostPersistEvent $event) { $em= $this-&gt;doctrine-&gt;getManager(); $product = $this-&gt;doctrine-&gt;getRepository('Acme\DemoBundle\Entity\Product')-&gt;findOneById($customId); $image = new Image(); $image-&gt;setProduct($product) -&gt;setName($uploadInfo-&gt;name) -&gt;setStoredName($uploadInfo-&gt;storedName) -&gt;setUuid($uploadInfo-&gt;uuid) -&gt;setSize($uploadInfo-&gt;size) -&gt;setMimeType($uploadInfo-&gt;mimeType); $validator = Validation::createValidatorBuilder() -&gt;enableAnnotationMapping() -&gt;getValidator(); $a = $product-&gt;getImages(); $a-&gt;add($image); $product-&gt;setImages($a); $errors = $validator-&gt;validate($product); </code></pre> <p>And I've got an error:</p> <pre><code>{"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":[]} </code></pre> <p>If let say I do NotNull annotation Assert on enother field (like name) - it works, I can get errors. But with ArrayCollection - is not. </p> <p>I am doing something wrong and can't find info in the internet.</p> <p>Could the gurus help me out?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload