php - The file could not be found while using LifecycleCallbacks -
i have problem form validation
in symfony2
.
in case $form->isvalid()
command results in the file not found.
though provide file during filling in form
additionally debuging of setfile
function in documents entity
leads conclusion file value set correctly. setfile function , results of print_r provided below:
/** * sets file. * * @param uploadedfile $file */ public function setfile(uploadedfile $file = null) { $this->file = $file; print_r($this->file); // [this test] // check if have old image path if (is_file($this->getabsolutepath())) { // store old name delete after update $this->temp = $this->getabsolutepath(); } else { $this->link = 'initial'; } }
print_r result:
symfony\component\httpfoundation\file\uploadedfile object ( [test:symfony\component\httpfoundation\file\uploadedfile:private] => [originalname:symfony\component\httpfoundation\file\uploadedfile:private] => firefox.exe [mimetype:symfony\component\httpfoundation\file\uploadedfile:private] => application/octet-stream [size:symfony\component\httpfoundation\file\uploadedfile:private] => 338032 [error:symfony\component\httpfoundation\file\uploadedfile:private] => 0 [pathname:splfileinfo:private] => c:\wamp\tmp\php9dc3.tmp [filename:splfileinfo:private] => php9dc3.tmp )
my questions why though file provided correctly form validator fails?
when comment out part of setfile()
function starts work resulting in move exception.
'
/** * sets file. * * @param uploadedfile $file */ public function setfile(uploadedfile $file = null) { $this->file = $file; /*if (is_file($this->getabsolutepath())) { // store old name delete after update $this->temp = $this->getabsolutepath(); } else { $this->link = 'initial'; }*/ }
my form class:
<?php namespace appbundle\form; use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface; use symfony\component\optionsresolver\optionsresolverinterface; class documentstype extends abstracttype { /** * @param formbuilderinterface $builder * @param array $options */ public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('marker') ->add('document_date', 'date', array('widget' => 'single_text', 'format' => 'yyyy-mm-dd')) ->add('file', 'file') ->add('notes', 'text', array('required' => "false")) ; }
my controller in is_valid function results in negative result:
/** * code aimed @ checking if book choseen , therefore whether further works may carried out */ $session = new session(); if(!$session->get("app_books_chosen_lp")) return new redirectresponse($this->generateurl('app_listbooks')); // authorization goes here $documents = new documents(); $form = $this->createform(new documentstype(), $documents); $form->add('save', 'submit', array('label' => 'dodaj dokument')); $form->handlerequest($request); if ($form->isvalid()) {
my documents class:
<?php namespace appbundle\entity; use doctrine\orm\mapping orm; use symfony\component\validator\constraints assert; use symfony\component\httpfoundation\file\uploadedfile; /** * @orm\entity * @orm\haslifecyclecallbacks * @orm\table(name="documents") */ class documents { /** * @orm\column(type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ protected $id; /** * @orm\manytoone(targetentity="books", inversedby="documents") * @orm\joincolumn(name="book_id", referencedcolumnname="id") */ protected $book; /** * @orm\column(type="string", length=220) */ protected $marker; /** * @orm\column(type="date", length=220) */ protected $document_date; /** * @orm\column(type="string", length=220) * @assert\file(maxsize="6000000") */ protected $link; /** * @orm\column(type="text") */ protected $notes; /** * id * * @return integer */ public function getid() { return $this->id; } /** * set marker * * @param string $marker * @return documents */ public function setmarker($marker) { $this->marker = $marker; return $this; } /** * marker * * @return string */ public function getmarker() { return $this->marker; } /** * set document_date * * @param \datetime $documentdate * @return documents */ public function setdocumentdate($documentdate) { $this->document_date = $documentdate; return $this; } /** * document_date * * @return \datetime */ public function getdocumentdate() { return $this->document_date; } /** * set link * * @param string $link * @return documents */ public function setlink($link) { $this->link = $link; return $this; } /** * link * * @return string */ public function getlink() { return $this->link; } /** * set notes * * @param string $notes * @return documents */ public function setnotes($notes) { $this->notes = $notes; return $this; } /** * notes * * @return string */ public function getnotes() { return $this->notes; } /** * set book * * @param \appbundle\entity\books $book * @return documents */ public function setbook(\appbundle\entity\books $book = null) { $this->book = $book; return $this; } /** * book * * @return \appbundle\entity\books */ public function getbook() { return $this->book; } /* * ### file upload process ### */ /** * @assert\file(maxsize="6000000") */ private $file; /** * stores temporay path */ private $temp; /** * sets file. * * @param uploadedfile $file */ public function setfile(uploadedfile $file = null) { $this->file = $file; print_r($this->file); // [this test] // check if have old image path if (is_file($this->getabsolutepath())) { // store old name delete after update $this->temp = $this->getabsolutepath(); } else { $this->link = 'initial'; } } /** * @orm\prepersist() * @orm\preupdate() */ public function preupload() { if (null !== $this->getfile()) { $this->link = $this->getfile()->guessextension(); } } /** * file. * * @return uploadedfile */ public function getfile() { return $this->file; } public function getabsolutepath() { return null === $this->link ? null : $this->getuploadrootdir().'/'.$this->id.'.'.$this->link; } public function getwebpath() { return null === $this->link ? null : $this->getuploaddir().'/'.$this->link; } protected function getuploadrootdir() { // absolute directory uploaded // documents should saved return __dir__.'/../../../web/'.$this->getuploaddir(); } protected function getuploaddir() { // rid of __dir__ doesn't screw // when displaying uploaded doc/image in view. return 'uploads/documents'; } /** * @orm\postpersist() * @orm\postupdate() */ public function upload() { if (null === $this->getfile()) { return; } // check if have old image if (isset($this->temp)) { // delete old image unlink($this->temp); // clear temp image path $this->temp = null; } // must throw exception here if file cannot moved // entity not persisted database // uploadedfile move() method $this->getfile()->move( $this->getuploadrootdir(), $this->id.'.'.$this->getfile()->guessextension() ); $this->setfile(null); } /** * @orm\preremove() */ public function storefilenameforremove() { $this->temp = $this->getabsolutepath(); } /** * @orm\postremove() */ public function removeupload() { if (isset($this->temp)) { unlink($this->temp); } } }
i have found out problem. have marked "link" column file , validator failing link text , not file.
my link code was:
/** * @orm\column(type="string", length=220) * @assert\file(maxsize="6000000") */ protected $link;
and line:
* @assert\file(maxsize="6000000")
should not there.
Comments
Post a Comment