doune1000 2017-07-05 04:58
浏览 30
已采纳

如何将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?"

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题