如何将symfony验证器违规作为多维数组?

By default symfony validator violations looks like this

  [0] => -message: "This value should not be blank."
         -propertyPath: "name"
  [1] => -message: "This value should not be blank."
         -propertyPath: "productVarieties[0].name"

How can I convert this to a multidimensional array, so that productVarieties[0] will be nested inside a root array key?

I've tried using the property access component, but it seems like property path is in the wrong format

foreach ($violations as $violation) {
    $this->propertyAccessor->setValue($errors, $violation->getPropertyPath(), $violation->getMessage());
}

Exception class: "Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException" message: "Cannot write property "name" to an array. Maybe you should write the property path as "[name]" instead?"

The problem is you are trying to add a object property to an array which can not be done.

According to the error the element 0 of array productVarieties is an array itself. It needs to be a object to assign a property to it.

For workaround you can initialize it as a stdclass object first like

$this->propertyAccessor->setValue($errors, 'productVarieties[0]', new \stdClass);

or you can replace productVarieties[0].name with productVarieties[0][name].

I haven't worked with symphony but it should help.