【发布时间】:2014-02-18 18:18:39
【问题描述】:
我正在尝试获取以嵌套形式存储的数据,但在调用 $builder->getData() 时,我总是得到 NULL。
有谁知道如何在嵌套表单中获取数据?
这是 ParentFormType.php:
class ParentFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('files', 'collection', array(
'type' => new FileType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false
);
}
}
文件类型.php
class FileType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
// Each one of bellow calls returns NULL
print_r($builder->getData());
print_r($builder->getForm()->getData());
die();
$builder->add('file', 'file', array(
'required' => false,
'file_path' => 'file',
'label' => 'Select a file to be uploaded',
'constraints' => array(
new File(array(
'maxSize' => '1024k',
))
))
);
}
public function setDefaultOptions( \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver )
{
return $resolver->setDefaults( array() );
}
public function getName()
{
return 'FileType';
}
}
谢谢!
【问题讨论】:
-
您要访问哪些数据?
-
子数据,在这种情况下,它是文件对象的 ArrayCollection。另外我会注意到真正的问题是我无法从我制作的表单文件字段扩展名中访问数据,这对于父表单来说工作正常,但正如上面的示例中发生的那样,我无法访问嵌套表单的数据。
标签: symfony symfony-forms symfony-2.3