phpannotationszend-framework3zend-form2

How can I implement a Zend Form entity with an array of elements, using Annotations?


I have a need for a form to record "sets of data", where each set consists of a few numbers. User has an option to add additional sets, up to however many is needed. In some cases up to 70 such sets are entered, introducing a need for an array.

enter image description here

Looking up Zend Form, I liked the approach where a form is created using the Annotations feature (https://docs.zendframework.com/zend-form/quick-start/#using-annotations), since it gives me a neat "object", which I can use for things like saving it directly to the database via i.e. using Doctrine, and then also retrieving it.

Question

How can I define a form in PHP using Zend Form, where the form has expandable set of elements, where I can manage that entity using Doctrine?


Solution

  • Use

    /**
     * @Annotation\ComposedObject({
     * "target_object":"Namespace\Entity\ElementInput",
     * "is_collection":"true"
     * });
     *
     * @var ElementInput[]
     */
    private $elements;
    

    Later to define the number of elements to show:

    $form->get("elements")->setCount($count);