I have a Zend Framework 2 project with Doctrine installed/configured. I am using the Zend Form annotations directly on entities to generate forms to display/edit entity data. I am running into a problem:
An entity (Vendor) with a collection of sub-entities (Territory) configured in a ManyToMany relationship. The doctrine relationship works fine, however the zend form annotations I have added
/*
* ...doctrine annotations...
* @Annotation\Type("Zend\Form\Element\Collection")
* @Annotation\Options({"label":"Territory", "target_element": {"type": "\DocurepVendor\Form\TerritoryFieldset"}})
*/
Create the fieldset object I've made, but do not name the field names as an array. (inputs are all named city instead of city[]) so when I submit the update form, the mapper expects an array but only finds a string and fails.
Here is my fieldset code.
TerritoryFieldset.php
namespace DocurepVendor\Form;
use \Zend\Form\Fieldset;
use \Zend\InputFilter\InputFilterProviderInterface;
class TerritoryFieldset extends Fieldset implements InputFilterProviderInterface {
public function __construct($name = null, $options = array()) {
parent::__construct('Locations', $options);
$this->setHydrator(new \Zend\Stdlib\Hydrator\ClassMethods())
->setObject(new \DocurepLocation\Entity\Territory());
$this->add(array(
'name' => 'city',
'options' => array(
'label' => 'City'
),
'attributes' => array()
))->add(array(
'name' => 'state',
'options' => array(
'label' => 'State'
),
'attributes' => array(
'required' => 'required'
)
));
}
public function getInputFilterSpecification() {
return array();
}
}
If I set the name in the fieldset to 'city[]' instead of 'city', the fieldset doesn't even populate with the doctrine data.
I'm not sure whether this is something I need to configure in the sub-element field set, the parent element's annotations, somewhere else entirely, or if this simply cannot be done and I need to implement this in another way. Can somebody give me a push in the right direction?