Symfony:每个关联只有一个文件实体和不同的验证规则? [英] Symfony: Only one file entity and different validation rules for each association?
问题描述
在我目前的项目中,我决定只创建一个可翻译的文件实体,并重用它所有的图像/文档属性。对于翻译,我使用Knp Doctrine Behaviors Translatable。
In my current project, I decided to create only one translatable File entity and reuse it for all image/document properties I have. For the translations, I uses Knp Doctrine Behaviors Translatable. So here is the code.
文件类:
class File
{
use ORMBehaviors\Translatable\Translatable;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __toString()
{
return (string)$this->id;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
}
可翻译文件类:
class FileTranslation
{
use ORMBehaviors\Translatable\Translation;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
public $path;
/**
* @Assert\File()
*/
private $file;
/*
* Non tracked parameter
*/
public $folder;
/**
* Set name.
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set path.
*
* @param string $path
*/
public function setPath($path)
{
$this->path = $path;
}
/**
* Get path.
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/**
* Set folder
*
* @param string $folder
*
* @return File
*/
public function setFolder($folder)
{
$this->folder = $folder;
return $this;
}
/**
* Get folder
*
* @return File
*/
public function getFolder()
{
return $this->folder;
}
}
然后举例说明如何在另一个实体(User)创建一个图片属性:
And then an example of how it's used in another entity (User) to create an image property:
class User
{
/**
* @ORM\OneToOne(targetEntity="File", cascade={"persist", "remove"})
* @ORM\JoinColumn(name="image_id", referencedColumnName="id", nullable=true)
* @Assert\Valid()
*/
private $image;
}
没有新的。我只是跟着Symfony / Knp文档,它工作正常。然而,现在我想添加不同的验证规则每次我创建一个新的属性,如$ image为不同的实体。这里最好的策略是什么?
Nothing new. I just followed Symfony/Knp documentation and it works fine. However, now I want to add different validation rules each time I create a new property like $image for different entities. What is the best strategy here?
例如,每当我尝试在$ image属性中添加与文件相关的验证规则时,它说它找不到一个文件。
Each time I try to add a validation rule related to file in the $image property, for instance, it says it cannot find a file.
推荐答案
对于有同样问题的人,我终于做了@sylvain说,我创建了我的自定义验证器:
For those who have the same problem, I finally did what @sylvain said and I created my custom validators:
http://symfony.com /doc/current/cookbook/validation/custom_constraint.html
这是一个很棒的教程,btw:
This is a great tutorial, btw:
https://knpuniversity.com/screencast/question -answer-day / custom-validation-property-path
它工作正常。但是,我仍然认为@valid约束应该工作,我不明白为什么它没有。
It works fine. However, I still think the @valid constraint should have worked and I don't understand why it didn't.
这篇关于Symfony:每个关联只有一个文件实体和不同的验证规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!