php - Symfony 嵌入式 ChoiceType 表单更新每个实体实例
问题描述
我有一个EditAnnouncementType
嵌入在表单中的CollectionType
表单。嵌入表单有两种ChoiceType
形式,一种可以正确更新映射的Announcement
实体。但是,另一个将尝试更新我的数据库中实体的所有实例,从而导致此错误。
类型错误:传递给 AppBundle\Entity\Announcement::setType() 的参数 1 必须是整数类型,给定 null,在 /Users/dperezpe/dev/grand-central/673/grand-central/vendor/symfony/ 中调用symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php 第 528 行
EditAnnouncementType.phptype
表单是错误之一。
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('edit', SubmitType::class,
array
(
'label' => 'Save changes',
'attr' => ['class' => 'btn btn-primary']
))
->add('type', ChoiceType::class,
[
'choices' =>
[
'info_type' => 1,
'star_type' => 2,
'alert_type' => 3,
'lightbulb_type' => 4,
'event_type' => 5,
'statement_type' => 6,
'cat_type' => 7,
'hands_type' => 8
],
'expanded' => true,
'multiple' => false,
'required' => true,
'label_attr' => array(
'class' => 'sr-only'
),
])
->add('audience', ChoiceType::class,
[
'choices' =>
[
'Students: anybody with a student level field populated' => 'students',
'Employees: anybody with an employee ID number' => 'employees'
],
'expanded' => true,
'required' => true,
'multiple' => true
])
集合类型
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('announcements', CollectionType::class,
[
'entry_type' => EditAnnouncementType::class,
'entry_options' => ['label' => false],
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => AnnouncementManager::class
]);
}
我怀疑这与呈现给它的 HTML 名称的差异有关,其中类型输入缺少一个空[]
<input type="radio"
id="announcement_edit_collection_announcements_221_type_0"
name="announcement_edit_collection[announcements][221][type]"
required="required" value="1">
<input type="checkbox" id="announcement_edit_collection_announcements_221_audience_0"
name="announcement_edit_collection[announcements][221][audience][]
"value="students">
解决方案
推荐阅读
- javascript - 我可以使用 detox 测试框架来测试 react + react 原生应用吗?
- php - Yii2 Facebook oauth - 违反完整性约束:1048 - 列“电子邮件”不能为空
- java - 转换 JavaObject `scala.collection.Map
- syntax - 为什么OCaml中有这么多表达式终止符?
- sas - 如何在 SAS 中创建动态字符串?
- json - 如何解析具有嵌套模式的 json?
- mysql - 我想从 SQL 中的表中更新数量,从检查 MIN(ExpDate) 和条形码
- sql - SQL 使用 WHERE LIKE 选择 DISTINCT
- php - 无法从 php 中的任何 HTTP 请求中获取参数
- java - 为什么,当我缩放多边形时,我的光强度会发生变化?