【问题标题】:How to use symfony callback constraint with validation group in yaml?如何在 yaml 中使用带有验证组的 symfony 回调约束?
【发布时间】:2015-07-13 01:55:36
【问题描述】:

我正在使用 Symfony 2.6,我正在按照这些教程学习如何使用验证回调约束:

http://symfony.com/blog/new-in-symfony-2-4-a-better-callback-constraint

http://symfony.com/doc/current/reference/constraints/Callback.html#external-callbacks-and-closures

为了调用外部验证调用,我尝试使用以下 yaml 配置:

App\APIBundle\Entity\Order:
properties:
    id:
        - Type:
            type: integer
            message: "Der Wert {{ value }} ist kein gültiger {{ type }}."
    amount:
        - Type:
            type: integer
            message: "Der Wert {{ value }} ist kein gültiger {{ type }}."
            groups: [ "AppOrder", "AppOrderbasket" ]
        - Callback: [App\APIBundle\Validator\Validator, validate]
            groups: [ "AppOrder", "AppOrderbasket" ]

我在尝试使用 external 回调验证类验证 amount 属性时遇到以下问题:

验证类 App\APIBundle\Validator\Validator 中的函数“validate”根本不会被调用。我试图通过将“组”属性添加到回调约束来添加验证组。这似乎无效,因为我收到此警告(警告:trim() 期望参数 1 为字符串,给定数组);

如果我删除“groups”属性,警告消失,但验证器仍未被调用。

有什么想法吗?

提前致谢
宁斯基

【问题讨论】:

    标签: validation symfony


    【解决方案1】:

    您现在将default option syntax 与正常语法混合使用。这行不通。

    如果您只需要指定默认选项(在Callback 约束的情况下为callback 选项),则可以使用Callback: [App\APIBundle\Validator\Validator, validate]。但是,如果您必须定义 2 个选项(在您的情况下为 callbackgroups),则必须使用正常语法:

    - Callback:
        callback: [App\APIBundle\Validator\Validator, validate]
        groups: [AppOrder, AppOrderbasket]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-13
      相关资源
      最近更新 更多