symfony/symfony — Issue #2496: [Form][Validator] Validation groups need improvement 
Author: canni
Created: 2011-10-27 11:59:06+00:00
State: open

Consider following example:

I have entity `Client` witch have field `type`, that field can hold 2 values (const TYPE_PERSON or TYPE_COMPANY), also I have 2 validation groups `person` and `company`, because ie. different fields are required based on client type.

Now standard way of creating new client looks like this:

```
public action clientCreateAction()
{
$client = new Entity\Client();
$form = $this->createForm(new ClientType(), $client);

$form->bindRequest($this->getRequest());

if($form->isValid())
// persis object
}
```

In this scenario, because validation is called by the `$form->bindRequest()` I'm not able to set correct validation groups without using `$data = $this->getRequest()->request->get($form->getName()); $type = $data['type'];` and injecting this value into `ClientType`

That process of determining validation groups based on *any* value should natural and easy with form/validation framework.

My suggest solution is to enable developer to use closure as value of `$options['validation_groups']` eg:

```
class ClientType extends AbstarctType
{
// ...
public function getDefaultOptions(array $options)
{
return array(
'validation_groups' => function($dataFromRequest, $objectTainedToType){
// return array of validation groups that should be used to validate given data / object pair
},
);
}
// ...
}
```