Php 在OneToMany关系中未验证Symfony文件类型
我有一个奇怪的现象,我们称之为表单验证中的事件 我有一个用户实体,它与文档实体(用于文件、图像、视频等上传)有一个OneToMany关系。提交表单时,不会验证文档实体的“文件”属性 让我们从用户实体开始,在这里我建立了与文档实体的关系:Php 在OneToMany关系中未验证Symfony文件类型,php,forms,validation,symfony,Php,Forms,Validation,Symfony,我有一个奇怪的现象,我们称之为表单验证中的事件 我有一个用户实体,它与文档实体(用于文件、图像、视频等上传)有一个OneToMany关系。提交表单时,不会验证文档实体的“文件”属性 让我们从用户实体开始,在这里我建立了与文档实体的关系: /** * @var Document * * @ORM\OneToMany(targetEntity="Document", mappedBy="user", cascade={"persist"}) **/ protected $documents
/**
* @var Document
*
* @ORM\OneToMany(targetEntity="Document", mappedBy="user", cascade={"persist"})
**/
protected $documents;
我在其中添加文档集合的用户表单类型:
->add('documents', 'collection', array(
'type' => new DocumentType(),
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,
'prototype' => true,
))
在该表单类型的setDefaultOptions方法中,我激活了:
'cascade_validation' => true,
现在让我们转到文档实体:
/**
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class Document
{
/**
* @var IntegerType
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
* @ORM\Column(type="string")
*/
protected $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
protected $path;
private $temp;
/**
* @Assert\NotBlank()
* @Assert\File(maxSize="6000000")
*/
protected $file;
/**
* @var User
* @ORM\ManyToOne(targetEntity="User", inversedBy="documents")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=false)
* */
protected $user;
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
// check if we have an old image path
if (isset($this->path)) {
// store the old name to delete after the update
$this->temp = $this->path;
$this->path = null;
} else {
$this->path = 'initial';
}
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->getFile()) {
// do whatever you want to generate a unique name
$filename = sha1(uniqid(mt_rand(), true));
$this->name = $filename;
$this->path = $filename.'.'.$this->getFile()->guessExtension();
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->getFile()) {
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->getFile()->move($this->getUploadRootDir(), $this->path);
// check if we have an old image
if (isset($this->temp)) {
// delete the old image
unlink($this->getUploadRootDir().'/'.$this->temp);
// clear the temp image path
$this->temp = null;
}
$this->file = null;
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
$file = $this->getAbsolutePath();
if ($file) {
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 up
// when displaying uploaded doc/image in the view.
return 'uploads/documents';
}
}
以及我添加文件的表单类型(DocumentType):
$builder
->add('file', 'file')
;
到这里为止,没关系。但当我提交表单时,“文件”字段未经验证
我已经在字段上方尝试了@Assert\Valid,但它仍然不起作用(我认为cascade\u验证无论如何都会取代它)
为什么文件没有经过验证?这与生命周期回调有关吗?我有Symfony官方烹饪书条目的“文档实体”。您有:
启用注释:true
?是的,我已经启用了它。您有没有测试任何验证错误<代码>/**@Assert\False()*/private$x=true代码>。再次阅读另一篇文章,你的数据对象不应该做任何上传逻辑:很难理解你的代码。代码来自Symfony Official cookbook条目。我添加了另一个字段,比如你的私有$x=true。它得到验证,突然,“文件字段”也得到验证。但当我没有“文件”以外的其他字段时,它不会验证它:/strange*当我没有其他字段时,它也会添加到DocumentType中