I want to know how can I validate array of arrays in symfony. My validation rules are:
So far I have done this:
$validator = Validation::createValidator();
$constraint = new Assert\Collection(array(
        'user' => new Assert\NotBlank(),
        'date' => new Assert\Date(),
        'present' => new Assert\NotBlank()
));
$violations = $validator->validate($request->request->get('absences')[0], $constraint);
But the problem is that it only allows to validate single array eg.$request->request->get('absences')[0].
Here is how the array looks like:

You have to put the Collection constraint inside All constraint:
When applied to an array (or Traversable object), this constraint allows you to apply a collection of constraints to each element of the array.
So, your code will probably look like this:
$constraint = new Assert\All(['constraints' => [
    new Assert\Collection([
        'user' => new Assert\NotBlank(),
        'date' => new Assert\Date(),
        'present' => new Assert\NotBlank()
    ])
]]);
Update: if you want to use annotations for this, it'll look something like this:
@Assert\All(
    constraints={
        @Assert\Collection(
            fields={
                "user"=@Assert\NotBlank(),
                "date"=@Assert\Date(),
                "present"=@Assert\NotBlank()
            }
        )
    }
)
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With