当我的上传表单在验证中遇到错误,并将用户发送回带有错误消息的表单时,它会丢失选择上传的文件,但如果我var_dump我的$entity->文件我可以看到它有该文件. ..
//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,)); }
将其放入树枝视图后,文件字段中没有任何内容.
这是我的实体……
<?PHPnamespace BizTV\MediaManagementBundle\Entity;use Doctrine\ORM\MapPing as ORM;use Symfony\Component\ValIDator\Constraints as Assert;/** * BizTV\MediaManagementBundle\Entity\Image * * @ORM\table(name="image") * @ORM\Entity * @ORM\HaslifecycleCallbacks */class Image{ /** * @var integer $ID * * @ORM\Column(name="ID",type="integer") * @ORM\ID * @ORM\GeneratedValue(strategy="auto") */ private $ID; /** * @var string $name * * @ORM\Column(name="name",type="string",length=255) * @Assert\NotBlank */ private $name; /** * @var integer $wIDth * * @ORM\Column(name="wIDth",type="integer") */ private $wIDth; /** * @var integer $height * * @ORM\Column(name="height",type="integer") */ private $height; /** * @ORM\Column(type="string",length=255,nullable=true) */ private $path; /** * @var object BizTV\BackendBundle\Entity\company * * @ORM\ManyToOne(targetEntity="BizTV\BackendBundle\Entity\company") * @ORM\JoinColumn(name="company",referencedColumnname="ID",nullable=false) */ protected $company; /** * @var object BizTV\MediaManagementBundle\Entity\Folder * * @ORM\ManyToOne(targetEntity="BizTV\MediaManagementBundle\Entity\Folder") * @ORM\JoinColumn(name="folder",nullable=true) */ protected $folder; /** * @Assert\file(maxSize="6000000") */ public $file; /** * @ORM\PrePersist() * @ORM\PreUpdate() */ 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(); } } /** * @ORM\PostPersist() * @ORM\PostUpdate() */ 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); } /** * @ORM\PostRemove() */ 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 BizTV\BackendBundle\Entity\company $company */ public function setCompany(\BizTV\BackendBundle\Entity\company $company) { $this->company = $company; } /** * Get company * * @return BizTV\BackendBundle\Entity\company */ public function getCompany() { return $this->company; } /** * Set folder * * @param BizTV\MediaManagementBundle\Entity\Folder $folder */ public function setFolder(\BizTV\MediaManagementBundle\Entity\Folder $folder = NulL) { $this->folder = $folder; } /** * Get folder * * @return BizTV\MediaManagementBundle\Entity\Folder */ public function getFolder() { return $this->folder; }}
形式:
<?PHPnamespace BizTV\MediaManagementBundle\Form;use Symfony\Component\Form\AbstractType;use Symfony\Component\Form\FormBuilder;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'; }}解决方法 出于安全考虑,您不能为上载字段设置文件.有关详细信息,请参见此处 How to set the value of a HTML file field? 总结
以上是内存溢出为你收集整理的symfony2文件因表单错误而丢失全部内容,希望文章能够帮你解决symfony2文件因表单错误而丢失所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)