表单错误导致 symfony2 文件丢失 [英] symfony2 file lost upon form error

查看:23
本文介绍了表单错误导致 symfony2 文件丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 symfony2 网站教程中的示例,我正在使用与学说相关的文件上传的标准实现.

I am using a standard implementation of file upload in connection with doctrine, as per the example on the symfony2 website tutorials.

当我的上传表单在验证中遇到错误,并将用户发送回表单并显示错误消息时,它会丢失选择上传的文件,尽管如果我 var_dump 我的 $entity->file 我可以看到它有文件...

When my upload form encounters an error in validation, and sends the user back to the form with error messages, it looses the file chosen for upload, although if I var_dump my $entity->file I can see that it has the file...

    //if form is valid, do some stuff... if not:
    else {

        //var_dump($entity->file); //This works, I get my file
        //die;

        //Get and check the folder chosen as parent
        $entity->setFolder( $this->checkFolderId($request->request->get('folder')) ); //will cause die() if folder doesn't belong to this company

        $folders = $this->getFolders();

        return $this->render('BizTVMediaManagementBundle:Image:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
            'folders' => $folders,
            'fileExists' => $fileExists,
        ));

    }

这个放到树枝视图后,文件字段中什么都没有.

After this is put to the twig view, there is nothing in the file field.

这是我的实体...

<?php

namespace BizTVMediaManagementBundleEntity;

use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;

/**
 * BizTVMediaManagementBundleEntityImage
 *
 * @ORMTable(name="image")
 * @ORMEntity
 * @ORMHasLifecycleCallbacks
 */
class Image
{
    /**
     * @var integer $id
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string $name

     *
     * @ORMColumn(name="name", type="string", length=255)
     * @AssertNotBlank
     */
    private $name;

    /**
     * @var integer $width
     *
     * @ORMColumn(name="width", type="integer")
     */
    private $width;

    /**
     * @var integer $height
     *
     * @ORMColumn(name="height", type="integer")
     */
    private $height;

    /**
     * @ORMColumn(type="string", length=255, nullable=true)
     */
    private $path;

    /**
    * @var object BizTVBackendBundleEntitycompany
    *  
    * @ORMManyToOne(targetEntity="BizTVBackendBundleEntitycompany")
    * @ORMJoinColumn(name="company", referencedColumnName="id", nullable=false)
    */
    protected $company;     

    /**
    * @var object BizTVMediaManagementBundleEntityFolder
    *  
    * @ORMManyToOne(targetEntity="BizTVMediaManagementBundleEntityFolder")
    * @ORMJoinColumn(name="folder", referencedColumnName="id", nullable=true)
    */
    protected $folder;


    /**
     * @AssertFile(maxSize="6000000")
     */
     public $file;


    /**
     * @ORMPrePersist()
     * @ORMPreUpdate()
     */
    public function preUpload()
    {
        if (null !== $this->file) {
            // do whatever you want to generate a unique name
            $this->path = sha1(uniqid(mt_rand(), true)).'.'.$this->file->guessExtension();
        }
    }

    /**
     * @ORMPostPersist()
     * @ORMPostUpdate()
     */
    public function upload()
    {
        if (null === $this->file) {
            return;
        }

        // if there is an error when moving the file, an exception will
        // be automatically thrown by move(). This will properly prevent
        // the entity from being persisted to the database on error
        $this->file->move($this->getUploadRootDir(), $this->path);

        unset($this->file);
    }

    /**
     * @ORMPostRemove()
     */
    public function removeUpload()
    {
        if ($file = $this->getAbsolutePath()) {
            unlink($file);
        }
    }    

    public function getAbsolutePath()
    {
        return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path;
    }

    public function getWebPath()
    {
        return null === $this->path ? null : $this->getUploadDir().'/'.$this->path;
    }

    protected function getUploadRootDir()
    {
        // the absolute directory path where uploaded documents should be saved
        return __DIR__.'/../../../../web/'.$this->getUploadDir();

    }

    protected function getUploadDir()
    {
        // get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view.
        return 'uploads/images';
    }


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

    /**
     * Set name
     *
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

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

    /**
     * Set width
     *
     * @param integer $width
     */
    public function setWidth($width)
    {
        $this->width = $width;
    }

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

    /**
     * Set height
     *
     * @param integer $height
     */
    public function setHeight($height)
    {
        $this->height = $height;
    }

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

    /**
     * Set path
     *
     * @param string $path
     */
    public function setPath($path)
    {
        $this->path = $path;
    }

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

    /**
     * Set company
     *
     * @param BizTVBackendBundleEntitycompany $company
     */
    public function setCompany(BizTVBackendBundleEntitycompany $company)
    {
        $this->company = $company;
    }

    /**
     * Get company
     *
     * @return BizTVBackendBundleEntitycompany 
     */
    public function getCompany()
    {
        return $this->company;
    }

    /**
     * Set folder
     *
     * @param BizTVMediaManagementBundleEntityFolder $folder
     */
    public function setFolder(BizTVMediaManagementBundleEntityFolder $folder = NULL)
    {
        $this->folder = $folder;
    }

    /**
     * Get folder
     *
     * @return BizTVMediaManagementBundleEntityFolder 
     */
    public function getFolder()
    {
        return $this->folder;
    }


}

还有形式:

<?php

namespace BizTVMediaManagementBundleForm;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilder;

class ImageType extends AbstractType
{
    function __construct($createAction=0) {
        $this->createAction = $createAction;    
    }   

    public function buildForm(FormBuilder $builder, array $options)
    {

        $createAction = $this->createAction;

        if ($createAction) {        
            $builder
                ->add('file')
            ;
        }

        $builder
            ->add('name', 'text', array('label' => 'Namn'))
        ;
    }

    public function getName()
    {
        return 'biztv_mediamanagementbundle_imagetype';
    }
}

推荐答案

出于安全考虑,您不能为上传字段设置文件.请参阅此处了解更多信息.如何设置HTML文件字段的值?

You can't, for security purposes, set a file for the upload field. See here for more info. How to set the value of a HTML file field?

这篇关于表单错误导致 symfony2 文件丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆