嘿,我有一个问题,我要如何解决这个问题:
the form's view data is expected to be
an instance of class Symfony\Component\HttpFoundation\File\File,
but is a(n) resource. You can avoid this error by setting the "data_class"
option to null or by adding a view transformer that transforms a(n)
resource to an instance of Symfony\Component\HttpFoundation\File\File.
当我尝试更新它时发生这种情况,但不适用于FileType :: class
这是我的代码:
<?php
namespace XInteractive\Bundle\LeadsBundle\Form;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use XInteractive\Bundle\LeadsBundle\Entity\Document;
class DocumentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', null)
->add('file', FileType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Document::class
]);
}
}
将data_class
设置为空!
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', null)
->add('file', FileType::class, array(
'data_class' => null));
}
显示您的控制者和实体的代码与此表单有什么关系
并阅读官方文档:
对我来说最好的方法:
->add('file', FileType::class, ['mapped' => false])
然后将自己的关系文件逻辑写入数据库。