2017-07-05 04:58
浏览 25


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?"

图片转代码服务由CSDN问答提供 功能建议


  [0  ] =>  -message:“此值不应为空。”
 [1] =>  -message:“此值不应为空。”
 -propertyPath:“productVarieties [0] .name”

如何将其转换为多维 数组,以便productVarieties [0]将嵌套在根数组键中?

我尝试过使用属性访问组件,但似乎属性路径的格式错误< / p>

 $ this-&gt; propertyAccessor-&gt; setValue($ errors,$ violation-&gt; getPropertyPath(),$ violation-  &gt; getMessage()); 

异常类: “Symfony \ Component \ PropertyAccess \ Exception \ NoSuchPropertyException” 消息:“无法将属性”名称“写入数组。也许您应该将属性路径写为”[name]“而不是?”

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongyin8991 2017-07-05 05:27

    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.

    点赞 打赏 评论

相关推荐 更多相似问题