【问题标题】:Can't embed File Form to another Form In Symfony 2.8无法将文件表单嵌入到 Symfony 2.8 中的另一个表单
【发布时间】:2016-01-22 13:34:35
【问题描述】:

这里的想法是向用户或其他实体添加文件(照片)。我决定这个文件可以有自己的实体媒体。

正如 symfony book 中解释的那样,我在实体 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;
    }


}

然后,我通过文件上传创建了 Media FormType:

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

现在,我想将此表单嵌入到我的用户表单中:

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

但在我的新用户视图中,结果如下:

一些细节: - 我在 symfony 2.8 - 我使用 symfony 引导到这部分 (http://symfony.com/doc/2.8/cookbook/controller/upload_file.html) 我知道我可以使用 VichUploaderBundle 但我想先学习。

在此先感谢您的帮助,我希望我说的足够清楚,我糟糕的英语不会造成太多的理解问题。

编辑:我尝试制作 MediaType 服务。

在我的 Media FormType 中,我添加了:

private $mediaService;

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

(如 symfony 书中所述)

在我的 app/config/services.yml 中:

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

    tags:
        - { name: form.type }

在我的 Utilisateur FormType 中,我添加:

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

现在,我有错误:

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

【问题讨论】:

    标签: php forms symfony


    【解决方案1】:

    字段类型"entity" 仅允许您从数据库中现有的实体中获取。在那里,您想使用您定义的 MediaType 表单类型。这样做:

    1) 将您的 MediaType 定义为服务。例如,使用 YML:

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

    2) 替换

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

    作者:

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

    这应该已经好一点了。现在您可以通过表单主题自定义表单的显示(查看the documentation for defining custom FormTypes

    【讨论】:

    • 嘿!谢谢你的帮助:) 好的,我有一个错误:“无法加载类型“媒体””。我用详细信息编辑了我的帖子。
    • 我也编辑过;尝试MediaType::class 而不是'media'。关于“服务”错误,您不需要创建一个新类,您只需要获取您的MediaType 并在您的 services.yml 中定义它。我会再次编辑。
    • 我编辑了服务部分以将服务定义包含在 YML 中。如您所见,我将AdminBundle\Form\MediaType 声明为服务,并添加标签form.type 以便Symfony 了解您定义了一个新的字段类型。有关服务容器的更多详细信息,请查看:symfony.com/doc/current/book/service_container.html
    • 我有新错误:可捕获的致命错误:传递给 AdminBundle\Form\MediaType::__construct() 的参数 1 必须是 AdminBundle\Form\MediaService 的实例,没有给出,在 /var/ 中调用www/erdf/app/cache/dev/appDevDebugProjectContainer.php 在第 294 行并已定义。我清除了缓存,但仍然无法正常工作。我用新的修改编辑了我的帖子
    • 删除您的 MediaType 中的 __construct() 和您的私有 $mediaService,您根本不需要它,因为您在将表单定义为服务时没有提出任何参数。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多