Q:

Adding a strategy to a Zend\Form\Element\Collection

Is there a way to add a Hydration strategy to a Zend\Form\Element\Collection element? I tried it the normal way:
$hydrator = new ClassMethods();
$hydrator->addStrategy('language', new LanguageStrategy($em));
$hydrator->addStrategy('items', new UnitItemsStrategy($em));
$this->setHydrator($hydrator);

With the element:
$this->add(array(
    'type' => 'Zend\Form\Element\Collection',
    'name' => 'items',
    'options' => array(
        'label' => 'Items',
        'count' => 1,
        'should_create_template' => true,
        'allow_add' => true,
        'target_element' => array(
            'type' => 'Application\Form\UnitItemFieldset',
        ),
    ),
));

But the hydrator strategy just never gets called. When I just remap it to another element like text it gets called. So it seems to have to do with the element being a Zend\Form\Element\Collection.

A:

Actually, they can be just added to collections as to any other element. In my case there have been several mistakes in using collections correctly:

did not call $form->prepare() in the view script
did not use ClassMethods hydrator for a while (due to debugging), but I do not know why this should be a problem (yet it was, but probably I am missing something else)

Another possible problem I just found out:

your method used by ClassMethods (e.g. setItems in my case) is not callable, then ZF will not call the hydrator strategy either

