我对子集合的值不能正确补充水分有疑问。我正在将zend Framework 3与理论2结合使用。
由于我将不得不复制大量代码,因此我将描述我在做什么,如果需要,我将使用代码进行更新。
我们有以下内容:
FacilityCategoriesFieldset,它是一个字段集,用于VillaFieldset中的Collection元素
$this->add([
'type' => 'Zend\Form\Element\Collection',
'name' => 'facilityCategories',
'options' => [
'count' => 1,
'allow_add' => true,
'allow_remove' => true,
'create_new_objects' => true,
'target_element' => [
'type' => FacilityCategoryFieldset::class,
],
],
]);
FacilitiesFieldset是一个字段集,用于FacilityCategoriesFieldset中的Collection元素
$this->add([
'type' => 'Zend\Form\Element\Collection',
'name' => 'facilities',
'options' => [
'count' => 1,
'allow_add' => true,
'allow_remove' => true,
'create_new_objects' => true,
'target_element' => [
'type' => FacilityFieldset::class,
],
],
]);
所以从上方看
VillaForm contains VillaFieldset that contains Collection of FacilityCategoriesFieldset which contains a Collection of FacilitiesFieldset
假设我们在实际的表单页面上有此示例。
- Row A of FacilityCategoriesFieldset
- Row 1 of FacilitiesFieldset
- Row 2 of FacilitiesFieldset
- Row 3 of FacilitiesFieldset
- Row B of FacilityCategoriesFieldset
- Row x of FacilitiesFieldset
- Row y of FacilitiesFieldset
- Row z of FacilitiesFieldset
我的问题是,提交表单后,尽管帖子数据是正确的,但水化后的结果将是这样的:
- Row A of FacilityCategoriesFieldset
- Row x of FacilitiesFieldset
- Row y of FacilitiesFieldset
- Row z of FacilitiesFieldset
- Row B of FacilityCategoriesFieldset
- Row x of FacilitiesFieldset
- Row y of FacilitiesFieldset
- Row z of FacilitiesFieldset
FacilityCategoriesFieldset的最后一行的FacilitiesFieldset的行用于FacilitiesFieldset的所有行。
仅当具有包含另一个集合的集合的字段集时,才会发生这种情况。有人暗示我可能做错了吗? 如果您需要,我会给您其他信息/代码。
非常感谢。
答案 0 :(得分:1)
您对Collection
的实现略有偏离。您不应该将Collection的类型指定为FQCN,而是实际的对象(字段集)。
尝试一下:
/**
* @var CustomFieldset
*/
protected $customFieldset;
public function __construct(CustomFieldset $customFieldset) {
$this->customFieldset = $customFieldset;
}
// init()
$this->add(
[
'type' => Collection::class,
'required' => true,
'name' => 'customFieldset',
'options' => [
'label' => 'Custom fieldsets',
'count' => 1,
'allow_add' => true,
'allow_remove' => true,
'should_create_template' => true,
'target_element' => $this->customFieldset,
],
]
);