Using the @ComposedObject Zend Framework 2 Form Annotation

Recently I started playing with Zend Framework 2 Form Annotations which certainly makes building forms much simpler (if you have not heard about this yet see Matthew Weier O’Phinney’s post and the ZF2 Documentation). After getting it up and running I found out about about the @ComposedObject annotation which seemed very useful, however it took a bit of fiddling to get it up and running, here’s how I did it.

The @ComposedObject annotation lets you create a fieldset inside your form which was created from one annotated class by using the form annotations from another class.

To set this up I created 2 classes User & Address:

Address is pretty straight forward but if you look at the annotations for User you will see the address member is annotated as being a ComposedObject of type Album\Model\Address.

In the controller you can now add some code to create the form:

And a view to display it

Note a whole fieldset can be displayed with the formCollection() view helper.

At this point when you view this action in the browser the form fields should all be present, however all the fields will be empty rather than containing the data from the object. The reason for this is the User & Address classes have been annotated to use the ArraySerializable Hydrator which requires getArrayCopy() & exchangeArray() methods to be defined.

The reason for choosing ArraySerializable instead of another Hydrator such as ObjectPropery or ClassMethods is that in order for fieldsets to be hydrated they need the extracted data from the composed object to be presented as a sub-array but these hydrators will just return the object as an element in the array. ArraySerializable lets us define the array that is returned.

For Address these 2 methods are simple as Address just contains simple fields:

However for User we need to call the methods of Address to get back/write a serialized sub array

And now it should all be working!

5 thoughts on “Using the @ComposedObject Zend Framework 2 Form Annotation

  1. Pingback: ZendForm Annotations mit einem ComposedObject - Zend Framework Magazin

  2. Thanks for your post but the plugin you use to wrap around the code somehow doesn’t work for me. It’d be great if you fix it 😛

    Btw, nice post. I just move the infrastructure to use zf2 as RESTful service so I don’t work with front end work on zend anymore 🙂

  3. Hi Tom,
    thanks a lot for your examples. They really help a lot in understanding.
    However there is one thing I couldn’t figure out so far: What can I do if the Object property is an Array (or in my case traversable collection)?
    Like if you would allow multiple addresses for your User object?

    Also regarding your problem with the other Hydrators: I have used the solution of the type annotation for each class. So your user would receive a @type(“UserForm”) annotation and then you create a simple UserForm class which in it’s constructor binds to a simple User Object. Just like in Michael Gallego’s blogpost http://www.michaelgallego.fr/blog/2012/07/04/new-zendform-features-explained/ only that your form elements are created with annotations.
    Once the form as an object set the bind and Hydrators will work recursively.

    Regards,
    Cris

  4. Thanks for your post, it’s kind of “old” but i didn’t find any other post about @composedobject that helped me as much as this one.

    Bye

Leave a Reply

Your email address will not be published. Required fields are marked *