如何重现这个:
1.创建某种类型的集合
<?php
$builder->add('foo', 'collection', array(
'type' => new BarType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'prototype' => true,
'prototype_name' => 'this_is_prototype',
'options' => array(
'data_class' => 'Acme\FooBundle\Entity\Bar'
),
));
2. 创建BarType
<?php
class BarType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
var_dump(array(
$builder->getForm()->getName() => $builder->hasParent()
));
$builder->add('bar', 'text');
// this actually is not relevant, just adding anything
// so that bar form is not empty
}
}
var_dump() 的结果
如果我们的集合包含 3 个 Bar 对象,那么结果将是:
array (size=1)
'this_is_prototype' => boolean true
array (size=1)
0 => boolean false
array (size=1)
1 => boolean false
array (size=1)
2 => boolean false
结论
在buildForm
$builder ->getParent() 将仅返回原型的父构建器。
问题/问题
我需要访问父表单来获取一些参数。为什么现有集合元素的父级被删除?有什么解决方法吗?