【问题标题】:The form's view data is expected to be an instance of class File, but is a(n) string表单的视图数据应该是类 File 的一个实例,但它是一个 (n) 字符串
【发布时间】:2017-01-30 11:05:00
【问题描述】:

我正在尝试显示一个链接以在 Symfony 中下载现有文件,但标题中出现错误。

我有一个托儿所,可以上传她的文件,然后我想展示给他们看。我已经制作了文档的这一部分:http://symfony.com/doc/current/form/create_form_type_extension.html。但这对我不起作用...

所以,这是我的扩展,我们在表单中添加了 2 个选项:

<?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;
        }
    }
}

服务定义:

vs_crm.document_type_extension:
        class: VS\CrmBundle\Form\Extension\DocumentTypeExtension
        tags:
          - { name: form.type_extension, extended_type: Symfony\Component\Form\Extension\Core\Type\FileType }

查看扩展:

{% 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 %}

我的表格:

<?php

namespace VS\CrmBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class NurseryDocumentsType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('docUrl', FileType::class, array(
                'label' => 'Choose a PDF file',
                'doc_path' => 'nurseryDocumentsPath',
                'doc_name' => 'docUrl'
            ))
            ->add('isMandatory')
            ->add('docType');
    }

而且我尝试了几种方法调用 form_widget 但都不成功...

我从文档中没有真正理解的是:好的,builForm 中的 doc_name 将使用我的 doc_url(这是保存在数据库中的文件的名称),但是 doc_path 呢?所以我做了这个(不要粗鲁:p):

// NurseryDocuments entity
    /**
     * @return string|null
     */
    public function getNurseryDocumentPath()
    {
        return __DIR__.'/../../../web/static/documents/NurseryDocuments'.$this->docUrl;
    }

小精度: 1)我试图在使用 CraueFormBundle 制作的多步骤表单的一个步骤中显示此文档,因此我无法将参数从控制器传递到我的流程。 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 或通过 添加将 a(n) 字符串转换为实例的视图转换器 Symfony\Component\HttpFoundation\File\File.

谢谢!

【问题讨论】:

    标签: php symfony


    【解决方案1】:

    请在FormType 中添加'data_class' =&gt; nulldocUrl 后尝试。

    NurseryDocumentsType.php

    ->add('docUrl', FileType::class, array(
        'label' => 'Choose a PDF file',
        'doc_path' => 'nurseryDocumentsPath',
        'doc_name' => 'docUrl',
        'data_class' => null
    ))
    

    【讨论】:

    • 现在我有 500 错误:注意:未定义的索引:DocumentTypeExtion.php 中的 doc_path ...
    【解决方案2】:

    好的,我已经制作了另一种形式:

    <?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;
        }
    }
    

    我与下载方法一起使用,你可以看到更高的showNurseryDocumentAction($document)。所以我需要一个 formType 来上传文件,另一个来显示/下载它们。希望它可以帮助某人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-01
      • 1970-01-01
      • 2017-02-14
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多