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)

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

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序