php - Symfony2, Edit Form, Upload Picture -
im new forum , symfony. after hours of searching don't found solution problem.
problem:
i have problem in edit form. create form works fine! have edit form projects. when change fields, title , submit. picture disappears, because haven't pick one....
i have select current picture every time, because not pre selected.
what need: 1. need preview of current picture above file upload button. 2. when change data of edit form, preview picture shouldn't change!
is there way can achieve this? need help, thx :)
my form looks 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 workaround here... }
edit
here entity
/** * @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 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; /** * 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; } /** * 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; } /** * 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; } /** * 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; } /** * 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; } /** * 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; } /** * 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; } /** * 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); } /** * pictures * * @return \doctrine\common\collections\collection */ public function getpictures() { return $this->pictures; } /** * override tostring() method return name of 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() { // absolute directory path uploaded documents should saved return $this->gettmpuploadrootdir() . $this->getid() . "/"; } protected function gettmpuploadrootdir() { // absolute directory path uploaded documents should saved return __dir__ . '/../../../../web/resources/images/project/'; } /** * @orm\prepersist() * @orm\preupdate() */ public function uploadpicture() { echo $this->picture; // file property can empty if field 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()); } }
you should check out http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html looks you've not added in there, it'd worth having look.
your entity missing of methods , properties needed, , looks form definition not quite right yet either. docs @ explaining beginner stuff make sure give them over.
Comments
Post a Comment