'PHP, Symfony 6, nested forms, get recursively validation form errors for all forms as array for rest api

I have REST API. I wrote some rest handlers to fill form. I have nested forms. CollectionType. I use constraints inside form. How to get recursively validation form errors for all forms as array. Using $deep = true, $flatten = false.

Form1Type contains Form2Type, Form2Type contains Form3Type etc.

$errors = $Form1Type->getErrors(true, false);

Array example

$errors = [
    'Form1.field1' => [
        'errors' => ['message1', 'message2']
    ],
    'Form1.field2' => [
        'errors' => ['message1', 'message2']
    ],
    'Form1.field2' => [
        'fields' => [
            'Form2.field1' => [
                'errors' => ['message1', 'message2']
            ],
            'Form2.field2' => [
                'errors' => ['message1', 'message2']
            ],
        ],
        'errors' => [
            'message1', // Collection must contain 5 items
            'message2'
        ]
    ]
];


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source