如何在yaml中使用symfony回调约束和验证组?

时间:2022-01-03 10:31:47

I'm using Symfony 2.6 and I'm following these tutorials how to use the validation callback constraint:

我正在使用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

To invoke an external validation call I'm trying to use following yaml configuration:

要调用外部验证调用,我正在尝试使用以下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" ]

I run into following problems when trying to validate the amount property with an external callback validation class:

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

The function "validate" within the validation class App\APIBundle\Validator\Validator doesn't get invoked at all. I've tried to add validation groups by adding the "groups" property to the callback constraint. This seems not to be valid as i get this warning (Warning: trim() expects parameter 1 to be string, array given);

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

If I remove the "groups" property the warning dissapears but the validator is still not invoked.

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

Any ideas?

Thanks in advance
ninsky

在此先感谢ninsky

1 个解决方案

#1


You are now mixing the default option syntax with the normal syntax. That doesn't work.

您现在将默认选项语法与普通语法混合使用。这不起作用。

If you only need to specify the default option (which is the callback option in case of the Callback constraint), you can use Callback: [App\APIBundle\Validator\Validator, validate]. However, if you have to define 2 options (in your case callback and groups), you have to use the normal syntax:

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

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

#1


You are now mixing the default option syntax with the normal syntax. That doesn't work.

您现在将默认选项语法与普通语法混合使用。这不起作用。

If you only need to specify the default option (which is the callback option in case of the Callback constraint), you can use Callback: [App\APIBundle\Validator\Validator, validate]. However, if you have to define 2 options (in your case callback and groups), you have to use the normal syntax:

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

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