Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2: entity asserts that accept an array of types?

I have an entity with the field $companies. This field has to store an array of Company objects. So I described assert this way:

@Assert\Type("Acme\MyBundle\Entity\Company")

But it's always invalid because from my form i receive array of Companies but this assert wants it be not array but just one Company.

So how to overcome this? I suppose that it has to be something like that:

@Assert\Array(Type("Acme\MyBundle\Entity\Company"))
like image 935
VitalyP Avatar asked Jun 27 '11 09:06

VitalyP


1 Answers

Since the question is tagged for Symfony2.x, for the sake of completeness I have to point out that the new validation constraint All introduced since version 2.1 can do the whole job.

For each array or traversable objects (e.g. a Doctrine ArrayCollection), you can do the following:

/**
 * @Assert\All({
 *     @Assert\Type(type="Acme\MyBundle\Entity\EntityType")
 * })
 */    
protected $arrayOfEntities;

So, the Symfony2.1 users that are reading your question should prefer this elegant and clean solution.

like image 158
JeanValjean Avatar answered Sep 28 '22 04:09

JeanValjean