数值字段的 Symfony Collection 约束

发布于 2025-01-13 14:00:39 字数 1131 浏览 1 评论 0原文

确保我的数组包含键 ['extension_attributes']['shipping_assignments'][0]['shipping'] 我想使用以下集合约束验证

Collection:
    allowExtraFields: true
    fields:
        extension_attributes:
            -   Collection:
                    allowExtraFields: true
                    fields:
                        shipping_assignments:
                            -   Collection:
                                    allowExtraFields: true
                                    fields:
                                        0:
                                            -   Collection:
                                                    allowExtraFields: true
                                                    fields:
                                                        shipping: ~                 
                         

但我在验证时出错,因为我使用数字键 0

Symfony\Component\Validator\Mapping\Loader\AbstractLoader::newConstraint():参数 #1 ($name) 必须是字符串类型,给定 int,在 /var/www/ht 中调用
ml/vendor/cleverage/process-bundle/Validator/ConstraintLoader.php 第 44 行

集合约束中第 44 行字段选项无法接受数字键

To make sure my array contains keys ['extension_attributes']['shipping_assignments'][0]['shipping']
I want to use the following Collection constraint validation

Collection:
    allowExtraFields: true
    fields:
        extension_attributes:
            -   Collection:
                    allowExtraFields: true
                    fields:
                        shipping_assignments:
                            -   Collection:
                                    allowExtraFields: true
                                    fields:
                                        0:
                                            -   Collection:
                                                    allowExtraFields: true
                                                    fields:
                                                        shipping: ~                 
                         

But i have on error on validation because i use the numeric key 0

Symfony\Component\Validator\Mapping\Loader\AbstractLoader::newConstraint(): Argument #1 ($name) must be of type string, int given, called in /var/www/ht
ml/vendor/cleverage/process-bundle/Validator/ConstraintLoader.php on line 44

fields option in Collection constraint cannot accept numeric key

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说不完的你爱 2025-01-20 14:00:39

更新

错误是由于约束加载器造成的
如果我使用 php 而不是 yaml 进行约束定义,它可以工作

$constraint = new Assert\Collection(
    [
        'fields' => [
            0 => [new Assert\NotBlank()]
        ]
    ]
);

不能工作

Collection:
    fields:
        0:  [NotBlank]

Update

Error is due to the constraint loader
if i use php and not yaml for constraint definition it works

Work

$constraint = new Assert\Collection(
    [
        'fields' => [
            0 => [new Assert\NotBlank()]
        ]
    ]
);

Not work

Collection:
    fields:
        0:  [NotBlank]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文