我已经在这个问题上坐了很长一段时间并决定寻求帮助。我有以下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
答案 0 :(得分:1)
尝试使用它:
->arrayNode('tab')
->prototype('array')
->children()
->scalarNode('title')->end()
->scalarNode('image')->end()
->end()
->end()
->end()
config.yml中的就是这样的:
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)
这与你的方法有点不同,但你会达到同样的目的。