我正在尝试通过使用配置文件而不是硬编码的东西来使我的应用程序变得灵活,并且在验证约束方面遇到了麻烦:
/**
* @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
您可以将所需的任何参数或服务注入验证器。