0

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.

HelloWorld
  • 418
  • 3
  • 15

2 Answers2

0

For ComposedObject you need to pass your object on "target_object", and if there is collection , then set "is_collection" key.

I have edited it as below,

 class TripsForm
  {
    /**
     * @Form\Name("numberOfTrips")
     * @Form\ComposedObject({"target_object":"Permits\Form\Model\Fieldset\numOfTrips", "is_collection": true})
     */
    public $numberOfTrips = null;


    }

Also it is good to use full path i.e: @Form\Type("Zend\Form\Element\Number")

Gautam Rai
  • 2,445
  • 2
  • 21
  • 31
  • Thanks for your reply, I gave it a go and muddled it around a bit but it still doesn't want to work properly :( Might be w'orth noting that in this blog it seems to be possible to work without using the "target_object" key http://devblog.x2k.co.uk/using-the-composedobject-zend-framework-2-form-annotation/ I wonder if it's not a subtle configuration somewhere rather than the way I'm using Annotations (in the way the annotations are interpreted by AnnotationBuilder) – HelloWorld Jun 13 '18 at 10:02
0

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)

HelloWorld
  • 418
  • 3
  • 15