symfony/symfony — Issue #2677: [Valiator] workflow for validation groups behaves different for Callbacks/Getters constraints than Properties constraints
Author: oscarballadares
Created: 2011-11-18 17:17:11+00:00
State: open

Hi all. When Callbacks and Getters have defined validation groups, Validator won't include/execute them when not defining a group to validate in the ``` Validator->validate() ``` method. As opposite, all Properties constraints will be included/executed If I set no validation group in ``` Validator->validate(). ```

See the next example:

```php

Me\MyBundle\Entity\Person:
properties:
name:
- NotBlank: { groups: [ admin_context ] }
lastname:
- NotBlank: ~

getters:
nicePerson:
- 'True': { message: 'You are not a nice person', groups: [ admin_context ] }

constraints:
- Callback:
methods: [ PersistOnlyOnWeekend ]
groups: [ admin_context ]

```

If in my FormType class I do:

```php

public function getDefaultOptions(array $options)
{
return array('validation_groups' => array('admin_context'));
}

```

Then validator will omit ``` 'lastname: NotBlank' ``` , this is the expected behaviour.

If I change it like this:

```php

return array('validation_groups' => array( ));

```

Then all properties constraints will be included, no matter if some of them has a validation group or not. It is like the validator says: 'You didn't give me any group, so what the hell, I'll wrap all properties constraints by default'.

But the actual issue is that this is not the same with Getters and Callbacks constraints. for the admin_context, they will be included, but omitted if I do not provide a specific group. ``` (validation_groups =>array()) ``` . It is like the validator says 'You didn't give me any group, so I won't include the getter and the callback because they are explicitly for admin_context'.

If I do not set any group to the getters and callbacks, then validator will include them (and all properties constraints) by default if no validation group is defined in the getDefaultOptions method.

```php
getters:
nicePerson:
- 'True': { message: 'You are not a nice person' }

constraints:
- Callback:
methods: [ PersistOnlyOnWeekend ]

```

```php

public function getDefaultOptions(array $options)
{
return array('validation_groups' => array());
}

```

So in the meanwhile I added a 'default' group to all constraints in order to include them all :

```php

Me\MyBundle\Entity\Person:
properties:
name:
- NotBlank: { groups: [ admin_context, default ] }
lastname:
- NotBlank: { groups: default }

getters:
nicePerson:
- 'True': { message: 'You are not a nice person', groups: [ admin_context, default ] }

constraints:
- Callback:
methods: [ PersistOnlyOnWeekend ]
groups: [ admin_context, default ]

--------

FormType class:

public function getDefaultOptions(array $options)
{
return array('validation_groups' => array('default'));
}

```

Regards!