php - 在创建表单之前添加选项以选择 EntityType 字段的标签
问题描述
我有一个包含两个 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
]
);
解决方案
您可以手动构建您的选择数组并使用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,
...
推荐阅读
- javascript - 如何将 HTML 页面中的 JS 变量值获取到控制器中的模型变量上
- android - Google 登录在某些设备上引发错误 16 和 12501
- python - 使用某些 nan 处理进行 xarray 重采样
- c# - 按年龄范围计算出生日期
- node.js - 辅助函数的返回值未被等待
- java - 从 SCROLL_TAB_LAYOUT JTabbedPane 中删除选项卡后,将剩余的选项卡滚动到可见
- html - div滚动,与父级
- visual-studio-code - VS Code:如何为 .cpp 文件而不是 .h 启用格式保存?
- angular - UI 组件未根据数据更改进行相应更新
- elasticsearch - 嵌套关键字字段上的 Elasticsearch 范围查询