简体   繁体   English

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

[英]Symfony3 pass form data to collection inside collection

I have some nested forms with CollectionType and seems that the data from the constructor is not passed to the 2nd nesting level. 我有一些带有CollectionType的嵌套表单,似乎构造函数中的数据没有传递给第二个嵌套级别。

I simplified my form classes, just with I think's important (if you want more info jut tell me in the comments). 我简化了我的表单类,只是我觉得很重要(如果你想要更多信息,请在评论中告诉我)。

The bottom level form is entirely generated depending on the Activity entity class: 底层表单完全根据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',
      ...
    );
  }
}

Over the ActivityServiceCreationType I have the next form that is just a collection of the previous one: 在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,
      ...
    ));
  }
}

Then I have the "main" form, with I create from the controller: 然后我有“主”形式,我从控制器创建:

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
    ])
  ;
}

From the controller I want to set the Activity objects to the ActivityServiceCreationType form, so the fields can be created. 从控制器我想将Activity对象设置为ActivityServiceCreationType表单,因此可以创建字段。 And I'm doing it like this: 而我这样做:

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

As you can see the data for the form is: 如您所见,表单的数据是:

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

The results for the dumps that I put in the code is the following: For the first dump, in the ActivityServiceCreationMultipleType I get an ArrayCollection of Activities 我在代码中放置的转储结果如下:对于第一个转储,在ActivityServiceCreationMultipleType中我得到一个活动的ArrayCollection 在此输入图像描述

witch is what is expected, no problem here, 女巫是预期的,这里没问题,

But in the second dump, in the ActivityServiceCreationType, I'm getting null . 但是在第二个转储中,在ActivityServiceCreationType中,我得到了null Here what I expected is an Activity entity for each one of the forms in the collection, right? 在这里,我期望的是集合中每个表单的Activity实体,对吧?

Can you tell me where I'm wrong? 你能告诉我哪里错了吗?

-- Edited to add more info: I've been trying to know when the data is "lost" and added some code to the event in the collection type. - 编辑添加更多信息:我一直在试图知道数据何时“丢失”并在集合类型中向事件添加了一些代码。 In the ActivityServiceCreationMultipleType changed the POST_SET_DATA this way: 在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);
});

Now the dump() in the ActivityServiceCreationMultipleType (that you see in the last code snippet) shows the array of activities. 现在ActivityServiceCreationMultipleType中的dump()(您在上一个代码片段中看到)显示了活动数组。 Directly the activities. 直接活动。 And the dump() in ActivityServiceCreationType is executed 36 times (one for each activity) with null... Don't know why it seems to be passing the data to the last embedded form, but the event can not get it. ActivityServiceCreationType中的dump()执行36次(每个活动一次),带有null ...不知道为什么它似乎将数据传递给最后一个嵌入的表单,但事件无法获取它。

EDIT 编辑

The configurateOptions of ActivityServiceCreationCollectionType must be: ActivityServiceCreationCollectionType的configurateOptions必须是:

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

The configureOptions of ActivityServiceCreationMultipleType. 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
    ));
  }

The configureOptions of ActivityServiceCreationType. 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
    ));
  }

In conclusion, you must always indicate every external property that you want to pass to the form in configureOptions 总之,您必须始终在configureOptions中指明要传递给表单的每个外部属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM