gpt4 book ai didi

Symfony 子实体仅被验证为类型,跳过它自己的验证

转载 作者:行者123 更新时间:2023-12-01 10:00:51 24 4
gpt4 key购买 nike

我正在单独使用 Symfony 验证器,没有表单组件。

我有一个包含子实体的实体,目前我可以验证该字段是子实体的一个实例,但我还需要它来验证子实体的约束。

#validation.yml
# This is the entity I'm validating against, it checks the type but doesn't then validate
# it against the child entity below.
Greg\PropertyBundle\Entity\Property:
properties:
property_id:
- NotBlank: ~
- Type:
type: string
addresses:
- All:
- Type:
type: Greg\PropertyBundle\Entity\Address

# child entity
Greg\PropertyBundle\Entity\Address:
properties:
city:
- NotBlank: ~
- Type:
type: string

为了调用验证器,我将它与 DI 一起传递到我的一项服务并执行此操作:

// Validate the data
$errorList = $this->validator->validate($data);

我也通过传入以下标志进行了尝试:

$errorList = $this->validator->validate($data, null, true, true);

最佳答案

默认情况下,不会为属性中的对象委派验证。如果你想调用验证处理子对象,那么您应该使用特定的约束“有效”。

因此您的验证脚本将是:

#validation.yml
# This is the entity I'm validating against, it checks the type but doesn't then validate
# it against the child entity below.
Greg\PropertyBundle\Entity\Property:
properties:
property_id:
- NotBlank: ~
- Type:
type: string
addresses:
- All:
- Type:
type: Greg\PropertyBundle\Entity\Address
# addresses is array of entities, so use "traverse" option to validate each entity in that array
- Valid: { traverse: true }

# child entity
Greg\PropertyBundle\Entity\Address:
properties:
city:
- NotBlank: ~
- Type:
type: string

您可以在此处找到有关“有效”约束的更多详细信息: http://symfony.com/doc/current/reference/constraints/Valid.html

关于Symfony 子实体仅被验证为类型,跳过它自己的验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16428722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com