Symfony配置TreeBuilder:无效的路径类型


Symfony config TreeBuilder: invalide type for path

我在这个问题上坐了很长一段时间,决定寻求帮助。我有以下的TreeBuilder:

$treeBuilder->root(0)
    ->children()
        ->arrayNode('tab')
            ->prototype('array')
                ->children()
                    ->scalarNode('title')->end()
                    ->scalarNode('image')->end()
                ->end()
            ->end()
        ->end()
    ->end();

这是我要验证的数组:

array(2) {
    [0]=>
    array(1) {
        ["tab"]=>
        array(2) {
          ["title"]=>
          string(18) "Tab title"
          ["image"]=>
          string(7) "img.png"
        }
    }
    [1]=>
      array(1) {
        ["tab"]=>
        array(2) {
          ["title"]=>
          string(18) "Tab title"
          ["image"]=>
          string(7) "img.png"
        }
    }
}

这不能验证,并且总是给我错误Invalid type for path "0.tab.title". Expected array, but got string。我使用2.4.3版本

<标题>后编辑

好了,我改变了数组的结构现在它是这样的

array(1) {
    ["tab"]=>
    array(2) {
        [0]=>
        array(4) {
          ["title"]=>
          string(18) "Gogoși cu zmoală"
          ["image"]=>
          string(7) "img.png"
          ["target_view_type"]=>
          string(1) "t"
          ["data"]=>
          string(9) "test data"
        }
        [1]=>
        array(4) {
          ["title"]=>
          string(18) "Gogoși cu zmoală"
          ["image"]=>
          string(7) "img.png"
          ["target_view_type"]=>
          string(1) "t"
          ["data"]=>
          string(9) "test data"
        }
    }
}

和TreeBuilder看起来像这样:

$treeBuilder->root(0)
->children()
    ->arrayNode('0')
        ->prototype('array')
            ->children()
                ->scalarNode('title')->end()
                ->scalarNode('image')->end()
                ->scalarNode('target_view_type')->end()
                ->scalarNode('data')->end()
            ->end()
        ->end()
    ->end()
->end();

这里是我得到的错误:Invalid type for path "0.0.title". Expected array, but got string

试着用这个:

->arrayNode('tab')
  ->prototype('array')
    ->children()
      ->scalarNode('title')->end()
      ->scalarNode('image')->end()
    ->end()
  ->end()
->end()

。它应该是这样的:

tab:
  - title: foo
    image: bar
  - title: boo
    image: har

这将验证:

'tab' => 
    array (size=2)
      0 => 
        array (size=2)
          'title' => string 'foo' (length=3)
          'image' => string 'bar' (length=3)
      1 => 
        array (size=2)
          'title' => string 'boo' (length=3)
          'image' => string 'har' (length=3)

这与你的方法有点不同,但我猜你会达到相同的效果。