【问题标题】:add option to select tag for EntityType field before creating the form在创建表单之前添加选项以选择 EntityType 字段的标签
【发布时间】:2020-04-21 05:23:46
【问题描述】:

我有一个包含两个 EntityType 字段的表单,第一个是类别,第二个是子类别,类别字段将包含我在数据库中拥有的所有类别,但我想再添加一个,即“所有类别”,我希望它是默认选择的,我该如何实现这一点..

$builder
            ->add(
                'category',
                EntityType::class,
                [
                    'label' => 'Catégorie',
                    'class' => Category::class,
                    'choice_label' => 'title',
                    'mapped' => false
                ]
            )

            ->add(
                'subCategory',
                EntityType::class,
                [
                    'label' => 'Sous-catégorie',
                    'class' => SubCategory::class,
                    'choice_label' => 'title',
                    'mapped' => false
                ]
            );

【问题讨论】:

    标签: php forms symfony


    【解决方案1】:

    您可以手动构建您的选择数组并使用ChoiceType,例如:

    // retrieve list of your categories in your controller or somewhere else
    // and pass it using the options. Add it also before the default option "All categories".
    $categories = $options['categories'];
    
    $builder->add('category', ChoiceType::class, [
        'choices' => $categories,
        ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-25
      • 1970-01-01
      • 2020-03-01
      • 1970-01-01
      • 2020-09-04
      • 2018-07-25
      相关资源
      最近更新 更多