CollectionType 字段条目的值为空

问题描述 投票:0回答:0

我有一个包含多个字段的表格

ParentFormType
,其中一个是
CollectionType
类型。

此 CollectionType 字段是自定义 FormType

FilePhotoType
的集合,它本身包含多个 FormType 元素(按钮、HiddenFields、...)

formTypes/Properties 都没有映射到真实实体。

现在,当提交

ParentForm
时,集合字段的子值(数组)与
FilePhotoType
原型条目的副本一样多 - 但每个值都是
null

很明显,现在 symfony 怎么可能使用

FilePhotoType
的孩子的哪个值?

问: 有没有一种方法可以在不使用

FilePhotoType.fileHolderBase64
的情况下将
uploadFiles
字段值“映射”为
DataTransformer
-Collection 数据数组中的值?

备注:

  • 传递给表单的数据不相关,只是来自前端的数据
  • 会很好,如果该值是所有
    FilePhotoType's
    children
  • 的数组
  • 如果我遍历 formType 的子级,则可以访问所需的数据 - 所以它就在那里

FormType 类代码

/**
 * ParentForm
 */

class ParentFormType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {

        $builder->add('uploadFiles', CollectionType::class, [
            'entry_type' => FilePhotoType::class,  // <-- custom formType with multiple fields
            'entry_options' => [
                'empty_data' => null,   // <-- can i "map" this to a child field of FilePhotoType?
            ],
            'allow_add'     => true,
            'prototype'     => true,
            // ...  
        ]);

    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => DocumentFileModel::class,   // <-- Mapped to a model
        ]);
    }
}
/**
 * FilePhotoType
 */
class FilePhotoType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        // Button 1
        $builder->add('chooseFile', ButtonType::class, [
            // ...
        ]);    
        // Button 2
        $builder->add('takePhoto', ButtonType::class, [
            // ...
        ]);    

        // The FIELD, which data I need in the parent collection
        $builder->add('fileHolderBase64', HiddenType::class, [
            // ...
        ]);
    }
}

提交后的结果

这是表单提交后表单数据的样子

$fileUploadForm = [
    // ...
    'viewData' => [
        'uploadFiles' => [
            1 => null,     // <-- this should be the value of "uploadFiles.fileHolderBase64[1]"
            2 => null,     // <-- this should be the value of "uploadFiles.fileHolderBase64[2]
        ]
    ]
]

这个问题对我没有帮助:Symfony: Get data from subform (CollectionType)

symfony-forms symfony6
© www.soinside.com 2019 - 2024. All rights reserved.