簡體   English   中英

Symfony2,編輯表單,上傳圖片

[英]Symfony2, Edit Form, Upload Picture

我是這個論壇和symfony的新手。 經過數小時的搜索,我沒有找到解決問題的方法。

問題:

我的編輯表單有問題。 創建表單工作正常! 我有一個項目的編輯表格。 當我更改某些字段時,例如標題,然后提交。 圖片消失了,因為我沒有選一張。

我必須每次都選擇當前圖片,因為它不是預先選擇的。

我需要:1.我需要在文件上傳按鈕上方預覽當前圖片。 2.當我更改編輯表單的數據時,預覽圖片不應更改!

有沒有辦法可以做到這一點? 我需要你的幫助,謝謝

我的表格如下所示。

    public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
            ->add('title', 'text', array('attr' => array(
                    'label' => 'Titel',
                    'class' => 'input-xxlarge'
        )))
            ->add('short', 'text', array('attr' => array(
                    'label' => 'Beschreibung',
                    'class' => 'input-xxlarge'
        )))
            ->add('content', 'text', array('attr' => array(
                    'label' => 'Inhalt',
                    'class' => 'input-xxlarge'
        )))
            ->add('category', 'choice', array('choices' =>
                array('Tuning' => 'Tuning', 'Gas' => 'Gas', 'Reparatur' => 'Reparatur'), 'required' => true), array('attr' => array(
                    'label' => 'Kategorie',
                    'class' => 'input-xxlarge'
        )))
            ->add('active', 'choice', array(
                'choices' => array('0' => 'Nein', '1' => 'Ja'),
                'preferred_choices' => array('Nein'),
                'attr' => array(
                    'label' => 'aktivieren',
                    'class' => 'input-small'
        )))
            ->add('picture', NULL, array('label' => 'Bild', 'data_class' => null, 'required' => false,
            ))
  //Need i workaround here...

}

編輯

這是我的實體

 /**
 * @ORM\OneToMany(targetEntity="Pictures", mappedBy="project")
 */
protected $pictures;

public function __construct() {
    $this->pictures = new ArrayCollection();
    $this->created = new \DateTime();
}

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

/**
 * @var string
 *
 * @ORM\Column(name="title", type="string", length=255)
 */
private $title;

/**
 * @var string
 *
 * @ORM\Column(name="short", type="text")
 */
private $short;

/**
 * @var string
 *
 * @ORM\Column(name="content", type="text")
 */
private $content;

/**
 * @var string
 *
 * @ORM\Column(name="category", type="string", length=255)
 */
private $category;

/**
 * @var string
 *
 * @Assert\File(maxSize = "1024k", mimeTypesMessage = "Please upload a valid Picture")
 * @ORM\Column(name="picture", type="string", length=255)
 */
private $picture;

/**
 * @var integer
 *
 * @ORM\Column(name="active", type="smallint")
 */
private $active;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="created", type="datetime")
 */
private $created;

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

/**
 * Set title
 *
 * @param string $title
 * @return Project
 */
public function setTitle($title) {
    $this->title = $title;

    return $this;
}

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

/**
 * Set short
 *
 * @param string $short
 * @return Project
 */
public function setShort($short) {
    $this->short = $short;

    return $this;
}

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

/**
 * Set content
 *
 * @param string $content
 * @return Project
 */
public function setContent($content) {
    $this->content = $content;

    return $this;
}

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

/**
 * Set category
 *
 * @param string $category
 * @return Project
 */
public function setCategory($category) {
    $this->category = $category;

    return $this;
}

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

/**
 * Set picture
 *
 * @param string $picture
 * @return Project
 */
public function setPicture($picture) {
    $this->picture = $picture;

    return $this;
}

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

/**
 * Set active
 *
 * @param integer $active
 * @return Project
 */
public function setActive($active) {
    $this->active = $active;

    return $this;
}

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

/**
 * Set created
 *
 * @param \DateTime $created
 * @return Project
 */
public function setCreated($created) {
    $this->created = $created;

    return $this;
}

/**
 * Get created
 *
 * @return \DateTime 
 */
public function getCreated() {
    return $this->created;
}


/**
 * Add pictures
 *
 * @param \pspiess\ContentBundle\Entity\Pictures $pictures
 * @return Project
 */
public function addPicture(\pspiess\ContentBundle\Entity\Pictures $pictures)
{
    $this->pictures[] = $pictures;
    $pictures->setProject($this);
    return $this;
}

/**
 * Remove pictures
 *
 * @param \pspiess\ContentBundle\Entity\Pictures $pictures
 */
public function removePicture(\pspiess\ContentBundle\Entity\Pictures $pictures)
{
    $this->pictures->removeElement($pictures);
}

/**
 * Get pictures
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getPictures()
{
    return $this->pictures;
}

/**
 * Override toString() method to return the name of the project title
 * @return string title
 */
public function __toString()
{
    return $this->title;
}

public function getFullPicturePath() {
    return null === $this->picture ? null : $this->getUploadRootDir() . $this->picture;
}

protected function getUploadRootDir() {
    // the absolute directory path where uploaded documents should be saved
    return $this->getTmpUploadRootDir() . $this->getId() . "/";
}

protected function getTmpUploadRootDir() {
    // the absolute directory path where uploaded documents should be saved
    return __DIR__ . '/../../../../web/resources/images/project/';
}

/**
 * @ORM\PrePersist()
 * @ORM\PreUpdate()
 */
public function uploadPicture() {
    echo $this->picture;
    // the file property can be empty if the field is not required
    if (null === $this->picture) {
        return;
    }
    if (!$this->id) {
        $this->picture->move($this->getTmpUploadRootDir(), $this->picture->getClientOriginalName());
    } else {
        $this->picture->move($this->getUploadRootDir(), $this->picture->getClientOriginalName());
    }
    $this->setPicture($this->picture->getClientOriginalName());
}

/**
 * @ORM\PostPersist()
 */
public function movePicture() {
    if (null === $this->picture) {
        return;
    }
    if (!is_dir($this->getUploadRootDir())) {
        mkdir($this->getUploadRootDir());
    }
    copy($this->getTmpUploadRootDir() . $this->picture, $this->getFullPicturePath());
    unlink($this->getTmpUploadRootDir() . $this->picture);
}

/**
 * @ORM\PreRemove()
 */
public function deletePicture() {
    if (file_exists($this->getFullPicturePath())) {
        unlink($this->getFullPicturePath());
    }
    if (is_dir($this->getUploadRootDir())) {
        //rmdir($this->getUploadRootDir());
    }
}

您應該查看一下http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html ,好像您還沒有在其中添加任何內容,因此值得一看。

您的實體缺少某些所需的方法和屬性,看起來您的表單定義也不是很正確。 這些文檔非常擅長解釋初學者的內容,因此請確保您對他們有所了解。

解決我的問題。

您需要2個變量,一個字符串將路徑保存到數據庫中,另一個需要Assert \\ File類型用於der Upload。

這是代碼段:

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

/**
 * @Assert\File(maxSize = "1024k", mimeTypesMessage = "Please upload a valid Picture")
 */
private $picture;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM