symfony/symfony — Issue #2615: Something wrong with Constraints\Collection
Author: Koc
Created: 2011-11-11 09:05:24+00:00
State: open

The trivial (as I thinked before yesterday) problem: there is have some data in array `$data = array('exist_field' => '')` wich should be validated. If something wrong - return report like: `array('field1' => 'error1', 'field2' => 'error2');`

Variant one:

```php
array(
'exist_field' => new NotBlank(),
'not_exist_field' => new NotBlank(),
),
));

$result = $validator->validateValue($data, $constraintsCollection);
```

What I get

```
Array.[exist_field]:
This value should not be blank
Array.:
The fields "not_exist_field" are missing
```

so now I should by hand extract "not_exist_field" using `$violation->getMessageParameters()['{{ field }}']`. Not nice.

Variant two:

```php
array(
'exist_field' => new NotBlank(),
'not_exist_field' => new NotBlank(),
),
'allowMissingFields' => true
));

$validator->validateValue($data, $constraintsCollection);
```

What I get

```
Array.[exist_field]: This value should not be blank
```

Not enought.

Variant three:

```php
createFormBuilder(null, array(
'validation_constraint' => $constraintsCollection, // $constraintsCollection from variant one or two - no difference
'error_bubbling' => true,
))
->add('exist_field', 'text')
->add('not_exist_field', 'text')
->getForm();

$form->bind($data);

vd($form->isValid(), $form->getData(), $form->getErrors()); // false, array('exist_field' => null, 'not_exist_field' => null), array()
```

So form is not valid, but no any message about incorrects fields.

Variant four:

```php
createFormBuilder(null, array(
'error_bubbling' => true,
))
->add('exist_field', 'text', array('validation_constraint' => new NotBlank()))
->add('not_exist_field', 'text', array('validation_constraint' => new NotBlank()))
->getForm();

$form->bind($data);

vd($form->isValid(), $form->getData(), $form->getErrors()); // true, array('exist_field' => null, 'not_exist_field' => null), array()
```

Form is valid? o_O

IMHO the easiest way to fix it - add to `Collection` constraint option `replaceMissingFieldsWithNull`.