douhuanqiao5290 2019-03-21 16:44
浏览 40

Symfony嵌入式ChoiceType表单更新每个实体实例

I have an EditAnnouncementType form which is embedded in a CollectionType form. The embedded form has two ChoiceType forms, one which properly updates the mapped Announcement entity. The other one however, will attempt to update all instances of the entity in my database, resulting in this error.

Type error: Argument 1 passed to AppBundle\Entity\Announcement::setType() must be of the type integer, null given, called in /Users/dperezpe/dev/grand-central/673/grand-central/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 528

EditAnnouncementType.php The type form is the error one.

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
                      ])

The CollectionType

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
    ]);
}

I suspect it is related to the difference in the HTML name that was rendered to this, where the type inputs are missing an empty []

<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">
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用