Symfony表单的渲染问题 CollectionType 中新实体的 ChoiceType

Rendering issue for Symfony forms ChoiceType for a new entity in a CollectionType

提问人:Vincent Hoogstraten 提问时间:8/30/2023 最后编辑:DarkBeeVincent Hoogstraten 更新时间:8/30/2023 访问量:28

问:

在我的 Web 应用程序中,我可以创建/编辑/删除班次计划中的班次。这些班次将作为表加载。 现在的问题是,在我的日程安排中创建新班次(现有班次没有此问题)时,下拉列表呈现不正确:

第一行是现有班次,第二行显示新班次的呈现方式

我的表单如下所示:

轮班时间表:

$builder
            ->add('planningShifts', CollectionType::class, [
                'entry_type' => ShiftFormType::class,
                'entry_options' => [
                    'label' => false,
                    'numberOfWeeks' => $options["data"]->getNumberOfWeeks(),
                ],
                'label' => false,
                'allow_add' => true,
                'allow_delete' => true,
            ])

此计划中的单个班次 (ShiftFormType):

$weekChoices = [];
        for ($i = 1; $i <= $options['numberOfWeeks']; $i++) {
            $weekChoices[$i] = $i;
        }

$builder
        ->add('weeks', ChoiceType::class, [
                'choices' => $weekChoices,
                'required' => false,
                'multiple' => true,  //Note: When this value is false I don't have the render issue, but of course I need this to accept multiple options.
            ]);

我的树枝是这样呈现的:

<td class="cel-edit">
      {{ form_widget(shift.weeks) }}
   </td>`

我希望对于新的班次,下拉列表将正确加载。

Twig symfony-forms symfony5

评论

0赞 Marleen 9/1/2023
你能展示一下你用来添加新行的树枝/JavaScript 吗?在 CollectionType 表单中,新行通常是通过 JavaScript 添加的,因此,如果问题仅出在新行上,则可能在那里做错了什么。

答: 暂无答案