CollectionType中的空结果数组

时间:2018-02-15 09:52:27

标签: php symfony

请您解释如何在Symfony中使用CollectionType?

我有这样的测试:

public function testSubmitValidData()
{
    $formData = [
        'collection' => [
            1,
            2
        ],
        'name' => 'asd',
    ];
    $formBuilder = $this->factory->createBuilder();
    $formBuilder->add(
        'collection',
        CollectionType::class
    );
    $formBuilder->add(
        'name',
        TextType::class
    );

    $form = $formBuilder->getForm();
    $form->submit($formData);

    var_dump($form->getData());

    $this->assertTrue($form->isSynchronized());
    }
}

执行此测试后,collection部分形式中没有任何内容:

array(2) {
  'collection' =>
  array(0) {
  }
  'name' =>
  string(3) "asd"
}

请问,您能解释一下如何使用CollectionType吗?我应该写什么来在结果数组的collection键中显示数据?

谢谢。

1 个答案:

答案 0 :(得分:0)

尝试使用这样的方法,如果您为某个实体创建表单,则可以使用Collection类型进行某种关系

            $builder
            ->add(
                'name_from_relation_for_entity',
                CollectionType::class,
                [
                    'translation_domain' => 'invoicing',
                    'entry_type' => SomeType::class,
                    'allow_add' => true,
                    'allow_delete' => true,
                    'prototype' => true,
                    'prototype_name' => 'some__name__',
                    'error_bubbling' => false,
                    'by_reference' => false,
                    'label' => ' ',
                ]
            );