我试图通过使用配置文件而不是硬编码的东西来使我的应用程序灵活,但遇到了验证约束的问题:
/**
* @var ArrayCollection[SubjectTag]
*
* @ORM\OneToMany(targetEntity="SubjectTag", mappedBy="subject")
* @Assert\Count(max = 15, maxMessage = "You can't create more than 15 tags.")
* @Assert\Valid()
*/
protected $tags;
做这样的事情会更好:
/**
* @var ArrayCollection[SubjectTag]
*
* @ORM\OneToMany(targetEntity="SubjectTag", mappedBy="subject")
* @Assert\Count(max = "%subject.max_tags%", maxMessage = "You can't create more than %subject.max_tags% tags.")
* @Assert\Valid()
*/
protected $tags;
但是通过调试我发现我的参数从未恢复过。
有什么办法可以实现吗?
最佳答案
唯一的方法是创建自定义验证约束:
http://symfony.com/doc/current/cookbook/validation/custom_constraint.html
您可以将您需要的任何参数或服务注入(inject)验证器。
关于php - Symfony 将参数传递给验证器约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27467587/