Symfony Doctrine 验证不适用于 Embeddables 上的注释
Symfony Doctrine validation not working with Annotations on Embeddables
我只是想确保我的 @Assert\NotBlank 在 Embeddable 中的字段上在 phpunit-test 中工作(使用 Doctrine 2.7 和 Symfony 5.1)但是似乎没有进行检查。
为了确保它与测试环境无关,我用注入的验证器测试了同样的东西
在具有相同结果的测试服务器上的路线上。违反嵌入实体的断言工作得很好。
对为什么会出现这种情况有什么建议吗?
所以基本上:
/**
* @package App\Entity\Embeddables
* @ORM\Embeddable
*/
class MyEmbeddable
{
/**
* @var string
* @ORM\Column(type="string", nullable=false)
* @Assert\NotBlank
*/
private string $text;
}
和
/**
* @ORM\Entity(repositoryClass=MyThingRepository::class)
*/
class MyThing
{
//..ID-stuff
/**
* @var MyEmbeddable
* @ORM\Embedded(class="App\Entity\Embeddables\MyEmbeddable")
*/
private MyEmbeddable $embeddableTestVar;
}
会导致:
$myThing = new MyThing();
$validator = Validation::createValidatorBuilder()->enableAnnotationMapping()->getValidator();
dd($validator->validate($myThing));
正在打印一个空数组。
如有任何帮助,我们将不胜感激!
尝试添加 @Assert\Valid
注释
/**
* @ORM\Entity(repositoryClass=MyThingRepository::class)
*/
class MyThing
{
//..ID-stuff
/**
* @Assert\Valid
*
* @var MyEmbeddable
* @ORM\Embedded(class="App\Entity\Embeddables\MyEmbeddable")
*/
private MyEmbeddable $embeddableTestVar;
}
我只是想确保我的 @Assert\NotBlank 在 Embeddable 中的字段上在 phpunit-test 中工作(使用 Doctrine 2.7 和 Symfony 5.1)但是似乎没有进行检查。 为了确保它与测试环境无关,我用注入的验证器测试了同样的东西 在具有相同结果的测试服务器上的路线上。违反嵌入实体的断言工作得很好。
对为什么会出现这种情况有什么建议吗?
所以基本上:
/**
* @package App\Entity\Embeddables
* @ORM\Embeddable
*/
class MyEmbeddable
{
/**
* @var string
* @ORM\Column(type="string", nullable=false)
* @Assert\NotBlank
*/
private string $text;
}
和
/**
* @ORM\Entity(repositoryClass=MyThingRepository::class)
*/
class MyThing
{
//..ID-stuff
/**
* @var MyEmbeddable
* @ORM\Embedded(class="App\Entity\Embeddables\MyEmbeddable")
*/
private MyEmbeddable $embeddableTestVar;
}
会导致:
$myThing = new MyThing();
$validator = Validation::createValidatorBuilder()->enableAnnotationMapping()->getValidator();
dd($validator->validate($myThing));
正在打印一个空数组。
如有任何帮助,我们将不胜感激!
尝试添加 @Assert\Valid
注释
/**
* @ORM\Entity(repositoryClass=MyThingRepository::class)
*/
class MyThing
{
//..ID-stuff
/**
* @Assert\Valid
*
* @var MyEmbeddable
* @ORM\Embedded(class="App\Entity\Embeddables\MyEmbeddable")
*/
private MyEmbeddable $embeddableTestVar;
}