1
votes

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

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:

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:

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);

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

Any ideas?

Thanks in advance
ninsky

1

1 Answers

4
votes

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]
    groups: [AppOrder, AppOrderbasket]