dongwu3596 2016-01-22 13:34
浏览 53
已采纳

无法在Symfony 2.8中将文件表单嵌入到另一个表单中

The idea here is to add a file (photo ) to a user or other entity. I decided that this file could have its own entity Media.

As explained in the symfony book , I created the add file function in the entity Media:

<?php

namespace AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Media
 *
 * @ORM\Table(name="Media", indexes={@ORM\Index(name="fk_Media_Cat_Media1_idx", columns={"Cat_Media_ID"})})
 * @ORM\Entity
 */
class Media
{
    /**
     * @var string
     *
     * @ORM\Column(name="Libelle", type="string", length=45, nullable=true)
     */
    private $libelle;

    /**
     * @var integer
     *
     * @ORM\Column(name="ID", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var \AdminBundle\Entity\CatMedia
     *
     * @ORM\ManyToOne(targetEntity="AdminBundle\Entity\CatMedia")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="Cat_Media_ID", referencedColumnName="ID")
     * })
     */
    private $catMedia;


    /**
     * @ORM\Column(type="string")
     *
     *
     * @Assert\File(mimeTypes={ "application/image" })
     */
    private $file;



    /**
     * Set libelle
     *
     * @param string $libelle
     * @return Media
     */
    public function setLibelle($libelle)
    {
        $this->libelle = $libelle;

        return $this;
    }

    /**
     * Get libelle
     *
     * @return string 
     */
    public function getLibelle()
    {
        return $this->libelle;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set catMedia
     *
     * @param \AdminBundle\Entity\CatMedia $catMedia
     * @return Media
     */
    public function setCatMedia(\AdminBundle\Entity\CatMedia $catMedia = null)
    {
        $this->catMedia = $catMedia;

        return $this;
    }

    /**
     * Get catMedia
     *
     * @return \AdminBundle\Entity\CatMedia 
     */
    public function getCatMedia()
    {
        return $this->catMedia;
    }



    public function setfile($file)
    {
        $this->file = $file;

        return $this;
    }


    public function getFile()
    {
        return $this->file;
    }


}

Then, i created the Media FormType with the file upload:

<?php

namespace AdminBundle\Form;

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

class MediaType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('libelle')
            ->add('catmedia','entity',  array('class'=>'AdminBundle:CatMedia',
                'property'=>'libelle'))
            ->add('file', FileType::class, array('label' => 'Image (Png or jpg file)'))
        ;
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AdminBundle\Entity\Media'
        ));
    }
}

And now, i want to embed this form in my user form:

<?php

namespace AdminBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class UtilisateurType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('nom')
            ->add('prenom')
            ->add('societe')
            ->add('mail')
            ->add('tel')
            ->add('password')
            ->add('catutilisateur','entity',  array('class'=>'AdminBundle:CatUtilisateur',
                'property'=>'libelle'))
            ->add('media','entity',  array('class'=>'AdminBundle:Media'))



        ;
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AdminBundle\Entity\Utilisateur'
        ));
    }
}

But in my new user view, this is the result:

enter image description here

some details: - i am in symfony 2.8 - i used the symfony boot to this part (http://symfony.com/doc/2.8/cookbook/controller/upload_file.html) I know i can use the VichUploaderBundle but i want to learn first.

Thanks in advance for your help, I hope I was clear enough , and my poor English will not create too many understanding problems.

Edit: i try to make MediaType service.

in my Media FormType, i added:

private $mediaService;

public function __construct(MediaService $mediaService)
{
    $this->mediaService = $mediaService;
}

(as explained in symfony book)

And in my app/config/services.yml:

admin.form.mediatype:
    class: AdminBundle\Form\MediaType

    tags:
        - { name: form.type }

In my Utilisateur FormType, i add:

 ->add('media', MediaType::class)

Now, i have the error:

Catchable Fatal Error: Argument 1 passed to AdminBundle\Form\MediaType::__construct() must be an instance of AdminBundle\Form\MediaService, none given, called in /var/www/erdf/app/cache/dev/appDevDebugProjectContainer.php on line 294 and defined
  • 写回答

1条回答 默认 最新

  • dsadsadsa1231 2016-01-22 15:33
    关注

    Field type "entity" only allows you to fetch from already existing entities in your database. Right there, you want to use the MediaType form type you defined. To do so:

    1) Define your MediaType as a service. For example, using YML:

        services:
            app.form.media:
                class: AdminBundle\Form\MediaType
                public: false
                tags:
                    -  { name: form.type }
    

    2) Replace

    ->add('media','entity',  array('class'=>'AdminBundle:Media'))
    

    by:

    ->add('media', MediaType::class)
    

    And this should be a bit better already. Now you can customize the display of your form through form themes (check the documentation for defining custom FormTypes)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题
  • ¥15 腾讯IOA系统怎么在文件夹里修改办公网络的连接
  • ¥15 安卓qpython向ksweb服务器post文件失败