我有一个像这样的DTO对象
class SaveManagedOfferCreativesCommand
{
/**
* @var int
*
* @Assert\NotNull()
* @Assert\Type(type="int")
*/
public $id;
/**
* @var EmbeddedCreative
*
* @Assert\Valid(groups={"defaultCreative"})
*/
public $defaultCreative;
/**
* @var EmbeddedCreative[]
*
* @Assert\Valid(groups={"creativeTranslations"})
*/
public $creatives = [];
public function setCreative(EmbeddedCreative $offerOfferWallCreative): void
{
$this->defaultCreative = $offerOfferWallCreative;
}
public function addOCreative(EmbeddedCreative $offerOfferWallCreative): void
{
$this->creatives[] = $offerOfferWallCreative;
}
}
和EmbeddedCreative类:
class EmbeddedCreative
{
/**
* @var string
*
* @Assert\NotNull(groups={"creativeTranslations", "defaultCreative"})
* @Assert\Type(type="string", groups={"creativeTranslations", "defaultCreative"})
*/
public $anchor;
/**
* @var string
*
* @Assert\NotNull(groups={"creativeTranslations"})
* @Assert\IsNull(groups={"defaultCreative"})
* @Assert\Type(type="string", groups={"creativeTranslations", "defaultCreative"})
* @Assert\Length(max=2, groups={"creativeTranslations", "defaultCreative"})
*/
public $language;
}
预期的行为是这样的:
取决于本文https://symfony.com/blog/new-in-symfony-3-4-groups-support-for-the-valid-constraint,我们可以对从symfony 3.4开始的组进行操作,但是对于组(我上面提供的代码),symfony根本不会验证可嵌入的类,也不会验证传递任何数据,但是当我删除组时,它也不会按预期进行验证(它将允许null还是非null $language
取决于可嵌入类中设置的约束)。
我认为与小组进行验证应该可以,并且我做错了什么。有人可以帮助我,提供进行预期验证的好方法吗?