簡體   English   中英

用參數定義約束的最佳方法

[英]Best way to define constraints with parameters

我嘗試在批注中使用參數,但是沒有用

例如在對象約束中:

...
use Symfony\Component\Validator\Constraints as Assert;

class SomeObj {

    /**
     * @Assert\File(maxSize="%max_filesize%")
     */
    private $file;

    ...

}

在parameters.yml中

parameters:
    ...
    max_filesize: 5M

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

我相信我必須放棄使用注釋格式,然后嘗試其他方法(YAML?)。 或使用新類擴展FileConstraint並通過參數以某種方式在其中設置$ maxFilesize。 這個問題還有哪些其他解決方案,哪個是最好的?

您應該使用{{ size }}而不是%max_filesize%

請參閱文檔參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM