Symfony3将表单数据传递给集合中的集合

时间:2019-06-14 10:48:04

标签: php symfony doctrine symfony-3.1

我有一些具有CollectionType的嵌套表单,并且似乎来自构造函数的数据没有传递到第二个嵌套级别。

我简化了表单类,只是我认为这很重要(如果您想获得更多信息,请在评论中告诉我)。

完全根据 Activity 实体类生成底层表单:

class ActivityServiceCreationType extends AbstractType {
  public function buildForm(FormBuilderInterface $builder, array $options) {

    $builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) use ($options, $router) {
      $activity = $event->getData();
      dump($activity); //JUST TO TEST

      $form = $event->getForm();
      ... //$form->add of all necessary fields
    }
  }

  public function configureOptions(OptionsResolver $resolver) {
    $resolver->setDefaults(array(
      'data_class' => 'AppBundle\Entity\Activity',
      ...
    );
  }
}

在ActivityServiceCreationType上,我具有下一个表单,该表单只是前一个表单的集合:

class ActivityServiceCreationMultipleType extends AbstractType {

  public function buildForm(FormBuilderInterface $builder, array $options) {

    $builder
      ->add('activities', CustomCollectionType::class, [
        'entry_type'    => ActivityServiceCreationType::class,
        'entry_options' => $options,
        'mapped'        => true,
        'allow_add'     => true,
        'show_add_link' => true,
      ])
    ;

    $builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) use ($options) {
      $data = $event->getData();
      dump($data); //To test the data arriving to this form

    });
  }

  public function configureOptions(OptionsResolver $resolver) {
    $resolver->setDefaults(array(
      'data_class' => null,
      ...
    ));
  }
}

然后我有了“主”表单,可以从控制器创建它:

class ActivityServiceCreationCollectionType extends AbstractType {
  public function buildForm(FormBuilderInterface $builder, array $options) {
  $builder
    ->add('selectAll', CheckboxType::class, [...])
    ...
  ;

  $builder->add('multipleActivities', CustomCollectionType::class, [
    'entry_type'    => ActivityServiceCreationMultipleType::class,
    'entry_options' => [
      "router"     => $options["router"],
      "em"         => $options['em'],
      "basePeriod" => $options['basePeriod'],
      'fit'        => $options['fit'],
      'periods'    => $options['periods'],
      'activities' => $options['activities']
    ],
    'mapped'        => true
    ])
  ;
}

我想从控制器中将Activity对象设置为ActivityServiceCreationType表单,以便可以创建字段。我正在这样做:

$form = $this->createForm(ActivityServiceCreationCollectionType::class,
  ["multipleActivities" => ["activities" => $activities]],
  [
    "router"     => $this->get("router"),
    "em"         => $this->getEm(),
    "periods"    => $periods,
    "basePeriod" => $basePeriod,
    'fit'        => $fit
  ]);

您会看到表格的数据是:

["multipleActivities" => ["activities" => $activities]]

我在代码中放入的转储结果如下: 对于第一个转储,在ActivityServiceCreationMultipleType中,我获得一个Activity的ArrayCollection enter image description here

是女巫,这没问题,

但是在第二个转储中,在ActivityServiceCreationType中,我得到。在这里,我期望的是集合中每个表单的一个Activity实体,对吧?

你能告诉我我哪里错了吗?

- 编辑以添加更多信息: 我一直想知道何时数据“丢失”,并在集合类型中向事件添加了一些代码。 在ActivityServiceCreationMultipleType中,以这种方式更改了POST_SET_DATA:

$builder->addEventListener(FormEvents::POST_SET_DATA, function (FormEvent $event) use ($options) {
  $data = $event->getData();
  dump($data);

  $form = $event->getForm();
  $form->get('activities')->setData($data);
});

现在,ActivityServiceCreationMultipleType(您在上一个代码片段中看到的)中的dump()显示了活动数组。直接活动。 并且ActivityServiceCreationType中的dump()被执行36次(每个活动一个),并且为null。 不知道为什么它似乎将数据传递给最后的嵌入式表单,但是该事件无法获取它。

1 个答案:

答案 0 :(得分:0)

编辑

ActivityServiceCreationCollectionType的configurateOptions必须为:

  public function configureOptions(OptionsResolver $resolver) {
    $resolver->setDefaults(array(
      'data_class' => null,
      'multipleActivities' => null
    );
  }

ActivityServiceCreationMultipleType的configureOptions。

public function configureOptions(OptionsResolver $resolver) {
    $resolver->setDefaults(array(
      'data_class' => null,
      'entry_options' => null,
      'router" => null,
      'em" => null,
      'basePeriod" => null,
      'fit' => null,
      'periods'=>null,
      'activities' => null
    ));
  }

ActivityServiceCreationType的configureOptions。

public function configureOptions(OptionsResolver $resolver) {
    $resolver->setDefaults(array(
      'data_class' => 'AppBundle\Entity\Activity',
      'entry_options' => null,
      'router" => null,
      'em" => null,
      'basePeriod" => null,
      'fit' => null,
      'periods'=>null,
      'activities' => null
    ));
  }

最后,您必须始终在configureOptions中指示要传递给表单的每个外部属性