Annontation约束属性路径

时间:2017-11-09 12:32:56

标签: symfony validation constraints entity propertypath

我想知道是否有可能将属性路径设置为应该应用断言的annontation约束。

考虑这个例子:

 /**
 * @ORM\OneToOne(targetEntity="Document", cascade={"persist"})
 * @Assert\Image(mimeTypes={"jpeg", "png"}, path="this.file")
 *
protected $document;

在此示例中,我想将Image约束应用于file属性,该属性是Document实体的子级,其属性类似于 path =“this.file”

这有可能吗?

2 个答案:

答案 0 :(得分:0)

我只能想到动态定义php中的约束。

use Symfony\Component\Validator\Constraints\Image;
// [...]
public function buildForm(FormBuilderInterface $builder, array $options)
    {
    $builder
        ->add('document', FileType::class, [
            'constraints' => [
               new Image([
                  'mimeTypes' => ["jpeg", "png"],
                  'path' => $anyVariable
               ])
            ],
        ]);

答案 1 :(得分:0)

您可以按照此doc中的说明制作自己的验证工具,然后应用于您想要的任何字段。

希望有所帮助