Symfony 3.4集合嵌入默认值

时间:2018-09-05 14:35:55

标签: symfony collections symfony-forms

将默认值传递给表单时遇到问题。它只是没有出现在表单中。 我试图遵循官方文档,但似乎配置正确。

$ facets_landing_page 是-> find($ id)中具有一对多关系的学说实体。 facetsLandingPage是$ facets_landing_page对象中集合的名称(包含很多)。

如果我将$ facets_landing_page作为“数据” 选项直接传递到-> add 函数中,它将显示在表单中,但是在保存提交时存在问题。 / p>

表单创建:

$formBuilder = $this->createFormBuilder($facets_landing_page)
  ->add('facetsLandingPage', FacetsLandingPageType::class);

然后是$form->createView()等。

自定义类型:

class FacetsLandingPageType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add(
            'facetsLandingPage', CollectionType::class, [
                'entry_type' => FacetsLandingPageDescriptionType::class,
                'entry_options' => [
                    'label' => false,
                ],
                'by_reference' => false,
                'allow_add' => true,
                'allow_delete' => true,
                'label' => false,
            ]
        );
    }

    public function getBlockPrefix() {
        return 'flpwrapper';
    }
}

子类型:

class FacetsLandingPageDescriptionType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder->add('language', LanguageSelectType::class);
        $builder->add('fec', FecSelectType::class, ['required' => false]);
        $builder->add('title', TextType::class);
        $builder->add('meta_title', TextType::class);
        $builder->add('meta_description', TextType::class);
        $builder->add('markdown', MarkdownType::class);
    }

    public function getBlockPrefix() {
        return 'flp';
    }

    public function configureOptions(OptionsResolver $resolver) {
        $resolver->setDefaults(
            [
                'data_class' => FacetsLandingPageDescription::class,
                'required' => false,
                'attr' => [
                    'class' => 'collection_item',
                ],
            ]
        );
    }

    public function buildView(FormView $view, FormInterface $form, array $options) {
        $view->vars['tab_title'] = 'New';
        if (!empty($form->getData())) {
            $view->vars['tab_title'] = $form->getData()->getTabTitle();
        }

        parent::buildView($view, $form, $options);
    }
}

2 个答案:

答案 0 :(得分:0)

您可以在集合声明中使用prototype_data属性。您可以修改以下内容。

        ->add(
            'collectionItems',
            CollectionType::class,
            [
                'entry_type'    => CollectionItemType::class,
                'prototype_data' => new CollectionItemType()
            ]
        )

答案 1 :(得分:0)

似乎FacetsLandingPageType并未从表单中获取任何默认值。所以我摆脱了它,然后将项目直接传递到主表单中

->add(
            'facetsLandingPage', CollectionType::class, [
                'entry_type' => FacetsLandingPageDescriptionType::class,
                'entry_options' => [
                    'label' => false,
                ],
                'by_reference' => false,
                'allow_add' => true,
                'allow_delete' => true,
            ]
        )