How can I change count of a Collection Fieldset in Zend Form when using Annotations?



Via using Zend\Form\Annotation, I can issue this directive:

 * @ComposedObject({
 *     "target_object":"Namespace\Model\ComposedCollection",
 *         "is_collection":"true",
 *         "options":{"count":2}
 *     }); 
private $property;

What this does is create a collection of ComposedCollection elements, in the above case, of size 2.

It's great when I need a static form with 2 elements, but how do I change it when I want to alter it dynamically? i.e. internally I want to be able to populate the form with 4 or 5 sets of data, with the number being unknown ahead of time.

Using Annotations is static, and not dynamic. Is there any way to do this?

I have tried using

$form->get("element_name")->setOption("count", $numericValue);

but it does not work, because I am guessing by the time I am using it, the form has already been built by the Annotation engine ($annotationBuilder->createForm(MyEntity::class);

Is there a way to rebuild the form?

I have tried $form->prepare()

but it only actually removes my Collection elements instead.

I have also tried $form->init() but it appears to do nothing.

I am in the process of rewriting the form NOT using Annotations, but that's not what I am trying to accomplish, since I am effectively losing my Entity by rewriting it in a programmatic way.

Show source
| php   | zend-framework   | zend-form   | zend-framework3   | zend-form2   2017-03-13 23:03 1 Answers

Answers ( 1 )

  1. 2017-03-14 00:03

    Use Collection::setCount() method.


    Also, as an alternative you can define fieldset programmatically. Place @Annotation\Exclude() on the $property and create a Fieldset like so:

        $c = new Element\Collection('collection');
            'label' => 'Collection',
            'count' => 1    ,
            'should_create_template' => true,
            'allow_add' => true,
            'target_element' => [
                'type' => ComposedCollectionFieldset::class

    Where ComposedCollectionFieldset contains the Elements that you need.

◀ Go back