我有一个包含多个字段的表格
ParentFormType
,其中一个是CollectionType
类型。
此 CollectionType 字段是自定义 FormType
FilePhotoType
的集合,它本身包含多个 FormType 元素(按钮、HiddenFields、...)
formTypes/Properties 都没有映射到真实实体。
现在,当提交
ParentForm
时,集合字段的子值(数组)与 FilePhotoType
原型条目的副本一样多 - 但每个值都是 null
!
很明显,现在 symfony 怎么可能使用
FilePhotoType
的孩子的哪个值?
问: 有没有一种方法可以在不使用
FilePhotoType.fileHolderBase64
的情况下将 uploadFiles
字段值“映射”为 DataTransformer
-Collection 数据数组中的值?
备注:
FilePhotoType's
children/**
* 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]
]
]
]