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

时间:2019-03-21 16:44:58

标签: php symfony symfony-forms

我有一个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.php type表单是错误之一。

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

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

我怀疑这与呈现给它的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">

0 个答案:

没有答案