symfony/symfony — Issue #3107: 'validation_constraint' - Problem with Collections
Author: DerStoffel
Created: 2012-01-13 09:59:05+00:00
State: open

I created the following form where I remove a field for certain cases, but when I add it, i want it to be validated, but only then.
If I would add the assert to the entity it actually would validate it even if it is not added by the validator, since the entity would
not match its "notBlank" assert.

```
public function buildForm(FormBuilder $builder, array $options) {
$builder->add('start', 'datetime', array('date_widget' => 'single_text', 'time_widget' => 'single_text'));
$builder->add('end', 'datetime', array('date_widget' => 'single_text', 'time_widget' => 'single_text'));
$builder->add('journeytime');
$builder->add('content');
if (count($this->secondaryCategories) > 0) {
$builder->add('secondaryCategory', 'entity', array('class' => 'ACMEContactBundle:SecondaryCategory', 'empty_value' => 'Bitte auswählen...'));
}
$builder->add('initiator', 'entity', array('class' => ACME\\MasterdataBundle\\Entity\\PartyInstance',
'query_builder' => $this->qb
));
}

public function getDefaultOptions(array $options) {
$collectionConstraint = new Collection(array(
'secondaryCategory' => new Assert\NotBlank()
));

if (count($this->secondaryCategories) > 0) {
return array(
'data_class' => 'ACME\ContactBundle\Entity\Visit',
'validation_constraint' => $collectionConstraint,
);
}

return array(
'data_class' => 'ACME\ContactBundle\Entity\Visit',
);
}
```

In general the form works, except, when I get to the validation, I get the following error:
Even though I felt, I followed the symfony2 docs.

```
Expected argument of type array or Traversable and ArrayAccess, object given
```

Bug or Stupidity? :)
Need help there.