2017-01-30 2 views
0

へのリンクを表示しようとしていますが、Symfonyにある既存のファイルをダウンロードしています。タイトル。フォームのビューデータはFileクラスのインスタンスであると予想されますが、(n)文字列です

私は彼女の書類をアップロードする託児所を持っており、その後私はそれらを見せたいと思います。私は文書のこの部分を作った:http://symfony.com/doc/current/form/create_form_type_extension.html

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

ビュー拡張しかし、それは

ので、これは我々がフォームに2つのオプションを追加し、私の拡張である...私のために動作しません。

{% 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いくつかの方法を呼び出すために試みたが、失敗した...

は、私が本当にドキュメントから理解していないことである:OK、builFormでdoc_nameは(私のdoc_urlを取るために起こっていますwhitchはdbに保存されたファイルの名前ですが)doc_pathはどうですか?だから私は、これが(失礼なことはありません:P):DIT

// 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に設定するか、または (n)文字列をSymfony \ Component \ HttpFoundation \ File \ Fileの インスタンスに変換するビュートランスフォーマーを追加することで、このエラーを回避できます。

ありがとうございます!

答えて

2

'data_class' => nulldocUrlに追加してから、FormTypeにお試しください。 [OK]を

NurseryDocumentsType.php

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

今私は500エラーがあります:通知:未定義のインデックス:DocumentTypeExtion.phpのdoc_path ... –

0

、私は次のように別の形をした:あなたはshowNurseryDocumentAction($document)高を見ることができるように私はダウンロード方式で使用

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

。だから私は、ドキュメントのアップロードとそれを表示/ダウンロードするために一つのformTypeが必要です。それが誰かを助けることができるといいですね。

関連する問題