Search code examples
phpannotationszend-framework2zend-formzend-form-fieldset

Zend 2 Annotations ComposedObject doesn't adopt fieldset attributes


I'm trying to create a zend form that references separate fieldsets entirely using annotations. I'm doing this using the ComposedObject annotation. But no annotations (like \type or \attributes) inside the fieldset classes seem to be added/used.

Only annotations from the parent form are used.

For example, if I were to add @Annotation\Type("number") to the parent Form class, then the input type would be correctly set to type="number". However, if I were to add @Annotation\Type("number") to the fieldset class, then nothing, nada, I get type="" instead. And I can't work out why!!

Here is my parent form:

<?php
namespace Permits\Form;

use Zend\Form\Annotation as Form;

/**
 * @Form\Name("trips")
 * @Form\Attributes({"method":"post"})
 * @Form\Type("Permits\Form\Form")
 */
class TripsForm
{


    /**
     * @Form\Name("numberOfTrips")
     * @Form\ComposedObject("Permits\Form\Model\Fieldset\numOfTrips")
     */
    public $numberOfTrips = null;


}

Here is the fieldset class Permits\Form\Model\Fieldset\numOfTrips:

<?php
namespace Permits\Form\Model\Fieldset;

use Zend\Form\Annotation as Form;
/**
 * @Form\Name("numOfTrips")
 * @Form\Attributes({
 *     "class": ""
 * })
 */
class numOfTrips
{

    /**
     * @Form\Attributes({
     *   "class" : "input--trips",
     * })
     * @Form\Options({
     *     "label": "",
     * })
     * @Form\Type("number")
     *
     */
    public $numOfTrips = null;

}

I'm creating the form using:

$builder = new AnnotationBuilder();
$form = $builder->createForm('Permits\Form\TripsForm');

I would be grateful for any help or direction.


Solution

  • Don't worry guys, I found a solution eventually

    The problem was not the way I was using annotations but rather, for some reason, the way I was rendering the form in the view.

    I was using $this->formRow($form->get('element')); for each element which should work in my opinion.

    Using just $this->form($form); instead seemed to fix it (no idea why)