用参数定义约束的最佳方式


Best way to define constraints with parameters

我试着在注释中使用参数,但在中不起作用

例如,在对象约束中:

...
use Symfony'Component'Validator'Constraints as Assert;
class SomeObj {
    /**
     * @Assert'File(maxSize="%max_filesize%")
     */
    private $file;
    ...
}

参数中。yml

parameters:
    ...
    max_filesize: 5M

我得了ConstraintDefinitionException('"%max_filesize%" is not a valid maximum size')

我认为我必须放弃使用注释格式,并尝试其他格式(YAML?)。或者用新类扩展FileConstraint,并通过参数设置$maxFilesize。这个问题的其他解决方案是什么?哪一个最好?

您应该使用{{ size }}而不是%max_filesize%

请参阅文档参考。