I have a complex data-type given by a class FormatInstance with a couple properties. Some of them with attributes like #[Assert\NotBlank].
I am using this class in an array in one of my entities like so:
#[ORM\Column(type: Types::JSON, nullable: true)]
#[Assert\All([
new AssertFormatInstance
])]
private ?array $formatInstances = [];
The AssertFormatInstance-assertion is meant to validate the interplay of the properties of my FormatInstances but I would also like to run each element of the array through "normal" validation that checks the non-custom assertion-attributes like the aforementioned Assert\NotBlank and so on.
But it turns out Assert\Valid cannot be nested into Assert\All and doing it like this:
#[ORM\Column(type: Types::JSON, nullable: true)]
#[Assert\All([
new AssertFormatInstance
])]
#[Assert\Valid]
private ?array $formatInstances = [];
doesn't do the trick either. (EDIT: it actually did the trick but the "real" bug was masking that problem. Still accepted the accepted answer due to the valuable info with regards to collections.)
Is it possible to make this work somehow or do I have to "manually" check everything in my AssertFormatInstanceValidator?
If you want to ensure that all element in your
$formatInstancesare type ofFormatInstance, you can use Type constraint in yourAllconstraint.Otherwise, I would recommend you to use a
Collectioninstead of anarrayand use theValidconstraint directly on the property (not in in theAllconstraint). As you can see in the documentation, it will validate each object in your collection if thetraverseoption is set to true (which is already true by default).