Search code examples
phparrayssymfonytraversal

Multi-dimensional array traversal with Symfony Form Validation


I have an array of multi-dimensional array that represents my form each level represents a child form until you get to a field.

I want to be able to traverse that array keeping track of where of where I have been so I don't go down the same route more than once.

Each array key is essentially a $form->get($key). There are a couple of fields that are named identically but are down different routes (highlighted in red in the below image).

Is there an easy way to achieve this?

enter image description here

Thanks


Solution

  • This is my fix. I created a form map which consists of the number of fields each with child arrays for the path to the element and the error.

    I then loop over them and pass them through Symfonys mapViolation method in Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapper.

    Here is complete class: https://gist.github.com/linxlad/3ec76c181f717fba532bf43484b7c970