Php 表格';s视图数据应为类文件的实例,但为(n)字符串
我试图显示一个链接,指向下载Symfony中的现有文件,但标题中有一个错误 我有一个托儿所可以上传她的文件,然后我想把它们给大家看。我已经制作了这部分文档:。但这对我不起作用 这是我的扩展,我们在表单中添加了两个选项:Php 表格';s视图数据应为类文件的实例,但为(n)字符串,php,symfony,Php,Symfony,我试图显示一个链接,指向下载Symfony中的现有文件,但标题中有一个错误 我有一个托儿所可以上传她的文件,然后我想把它们给大家看。我已经制作了这部分文档:。但这对我不起作用 这是我的扩展,我们在表单中添加了两个选项: <?php namespace VS\CrmBundle\Form\Extension; use Symfony\Component\Form\AbstractTypeExtension; use Symfony\Component\Form\Extension\Cor
<?php
namespace VS\CrmBundle\Form\Extension;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\PropertyAccess\PropertyAccess;
class DocumentTypeExtension extends AbstractTypeExtension
{
/**
* Returns the name of the type being extended.
*
* @return string The name of the type being extended
*/
public function getExtendedType()
{
return FileType::class;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefined(array('doc_path', 'doc_name'));
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
if(isset($options['doc_path']))
{
$parentData = $form->getParent()->getData();
$documentUrl = null;
if(null !== $parentData)
{
$accessor = PropertyAccess::createPropertyAccessor();
$documentUrl = $accessor->getValue($parentData, $options['doc_path']);
}
$view->vars['doc_path']-> $documentUrl;
}
if(isset($options['doc_name']))
{
$parentData = $form->getParent()->getData();
$documentName = null;
if(null !== $parentData)
{
$accessor = PropertyAccess::createPropertyAccessor();
$documentName = $accessor->getValue($parentData, $options['doc_name']);
}
$view->vars['doc_name']-> $documentName;
}
}
}
视图扩展:
{% extends 'form_div_layout.html.twig' %}
{% block file_widget %}
{% spaceless %}
{% if (doc_path is not null) and (doc_name is not null) %}
<a href="{{ asset(doc_path) }}">{{ doc_name }}</a>
{% endif %}
{% endspaceless %}
{% endblock %}
小精度:
1) 我试图用CraueFormBundle制作的多步骤表单的一个步骤来显示这些文档,因此我无法将参数从controlle传递到我的流。
2) 我试着做一个像这样的动作:
public function showNurseryDocumentAction($document)
{
$path = $this->get('kernel')->getRootDir().'/../web/static/documents/NurseryDocuments/';
$content = file_get_contents($path.$document);
$response = new Response();
$response->headers->set('Content-Type', 'mime/type');
$response->headers->set('Content-Disposition', 'attachment;filename="'.$document);
$response->setContent($content);
return $response;
}
这在另一个上下文中有效,但这里我有一个错误
完全错误:
表单的视图数据应该是类的实例
Symfony\Component\HttpFoundation\File\File,但为(n)字符串。你
可以通过将“data_class”选项设置为null或
添加将(n)字符串转换为实例的视图转换器
属于Symfony\Component\HttpFoundation\File\File
谢谢 请在
FormType
中使用docUrl
添加'data\u class'=>null
后重试
NurseryDocumentsType.php
->add('docUrl', FileType::class, array(
'label' => 'Choose a PDF file',
'doc_path' => 'nurseryDocumentsPath',
'doc_name' => 'docUrl',
'data_class' => null
))
请在
FormType
中使用docUrl
添加'data\u class'=>null
后重试
NurseryDocumentsType.php
->add('docUrl', FileType::class, array(
'label' => 'Choose a PDF file',
'doc_path' => 'nurseryDocumentsPath',
'doc_name' => 'docUrl',
'data_class' => null
))
好的,我制作了另一张表格,如:
<?php
namespace VS\CrmBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
class NurseryDocumentsShowType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('docUrl', TextType::class);
}
public function getParent()
{
return NurseryDocumentsType::class;
}
}
好的,我制作了另一个表单,如:
<?php
namespace VS\CrmBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
class NurseryDocumentsShowType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('docUrl', TextType::class);
}
public function getParent()
{
return NurseryDocumentsType::class;
}
}
现在我有500个错误:注意:documenttypextion.php中未定义的索引:doc\u路径…现在我有500个错误:注意:documenttypextion.php中未定义的索引:doc\u路径。。。
<?php
namespace VS\CrmBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
class NurseryDocumentsShowType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('docUrl', TextType::class);
}
public function getParent()
{
return NurseryDocumentsType::class;
}
}