My class, which I try to validate, has a property, which can be either an empty array or an array of certain objects:
<code>
use AppApiDtoWorkHour as WorkHourDto;
use SymfonyComponentSerializerAnnotationGroups as SerializerGroups;
use SymfonyComponentValidatorConstraints as Assert;
[....]
#[AssertAll(
new AssertType(type: WorkHourDto::class)
)]
#[AssertValid(groups: ['create', 'update'])]
#[SerializerGroups(['view', 'create', 'update'])]
public array $workHours = []
[....]
</code>
<code>
use AppApiDtoWorkHour as WorkHourDto;
use SymfonyComponentSerializerAnnotationGroups as SerializerGroups;
use SymfonyComponentValidatorConstraints as Assert;
[....]
#[AssertAll(
new AssertType(type: WorkHourDto::class)
)]
#[AssertValid(groups: ['create', 'update'])]
#[SerializerGroups(['view', 'create', 'update'])]
public array $workHours = []
[....]
</code>
use AppApiDtoWorkHour as WorkHourDto;
use SymfonyComponentSerializerAnnotationGroups as SerializerGroups;
use SymfonyComponentValidatorConstraints as Assert;
[....]
#[AssertAll(
new AssertType(type: WorkHourDto::class)
)]
#[AssertValid(groups: ['create', 'update'])]
#[SerializerGroups(['view', 'create', 'update'])]
public array $workHours = []
[....]
What would be the correct way to assert this? Currently the validation fails if the array is empty.
I have seen then When Constraint [https://symfony.com/doc/6.4/reference/constraints/When.html], but it seems that it is only for checking complex details.
Of course I can leave out the validation at all or only assert that it is an array, but that would be my last resort.
I am running symfony 6.4 on php8.2