Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
531 views
in Technique[技术] by (71.8m points)

php - Symfony2, Edit Form, Upload Picture

im new to this forum and to symfony. After hours of searching I don't found I solution for my problem.

Problem:

I have a problem in my edit form. The create Form works fine! I have an edit form for my projects. When I change some fields, like the title and then submit. The picture disappears, because I haven't pick one....

I have to select my current picture every time, because it is not pre selected.

What I need: 1. I need a preview of my current picture above the file upload button. 2. When I change data of my edit form, the preview picture shouldn't change!

Is there a way I can achieve this? I need your help, thx :)

My Form looks like this.

    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...

}

EDIT

here is my entity

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

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

/**
 * @var integer
 *
 * @ORMColumn(name="id", type="integer")
 * @ORMId
 * @ORMGeneratedValue(strategy="AUTO")
 */
private $id;

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

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

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

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

/**
 * @var string
 *
 * @AssertFile(maxSize = "1024k", mimeTypesMessage = "Please upload a valid Picture")
 * @ORMColumn(name="picture", type="string", length=255)
 */
private $picture;

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

/**
 * @var DateTime
 *
 * @ORMColumn(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 pspiessContentBundleEntityPictures $pictures
 * @return Project
 */
public function addPicture(pspiessContentBundleEntityPictures $pictures)
{
    $this->pictures[] = $pictures;
    $pictures->setProject($this);
    return $this;
}

/**
 * Remove pictures
 *
 * @param pspiessContentBundleEntityPictures $pictures
 */
public function removePicture(pspiessContentBundleEntityPictures $pictures)
{
    $this->pictures->removeElement($pictures);
}

/**
 * Get pictures
 *
 * @return DoctrineCommonCollectionsCollection 
 */
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/';
}

/**
 * @ORMPrePersist()
 * @ORMPreUpdate()
 */
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());
}

/**
 * @ORMPostPersist()
 */
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);
}

/**
 * @ORMPreRemove()
 */
public function deletePicture() {
    if (file_exists($this->getFullPicturePath())) {
        unlink($this->getFullPicturePath());
    }
    if (is_dir($this->getUploadRootDir())) {
        //rmdir($this->getUploadRootDir());
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should check out http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html it looks like you've not added anything from in there, so it'd be worth you having a look.

Your entity is missing some of the methods and properties needed, and it looks like your form definition is not quite right yet either. The docs are very good at explaining the beginner stuff so just make sure you give them a look over.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...