From ed63b7e53c805f2daf56ceae47affd332472528d Mon Sep 17 00:00:00 2001 From: serus Date: Mon, 14 Dec 2015 16:56:31 +0100 Subject: [PATCH 1/2] strings as array indexes example config: 'type' => 'Zend\Form\Element\Collection', 'options' => [ 'key_names' => ['sk', 'de', 'en'], ], --- src/Element/Collection.php | 42 +++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Element/Collection.php b/src/Element/Collection.php index 2d58b67f..4c0d9d1f 100644 --- a/src/Element/Collection.php +++ b/src/Element/Collection.php @@ -39,6 +39,13 @@ class Collection extends Fieldset */ protected $count = 1; + /** + * Initial key names array (optional parameter) + * + * @var array + */ + protected $keyNames = []; + /** * Are new elements allowed to be added dynamically ? * @@ -139,6 +146,10 @@ public function setOptions($options) $this->setCreateNewObjects($options['create_new_objects']); } + if (isset($options['key_names']) && is_array($options['key_names'])) { + $this->setKeyNames(array_values($options['key_names'])); + } + return $this; } @@ -195,6 +206,11 @@ public function populateValues($data) )); } + // Can't do anything with empty data + if (empty($data)) { + return; + } + if (!$this->allowRemove && count($data) < $this->count) { throw new Exception\DomainException(sprintf( 'There are fewer elements than specified in the collection (%s). Either set the allow_remove option ' @@ -301,6 +317,29 @@ public function getCount() return $this->count; } + /** + * Set the array key names + * + * @param $names + * @return Collection + */ + public function setKeyNames($names) + { + $this->keyNames = $names; + $this->setCount(count($this->keyNames)); + return $this; + } + + /** + * Get the array key names + * + * @return int + */ + public function getKeyNames() + { + return $this->keyNames; + } + /** * Set the target element * @@ -472,8 +511,9 @@ public function prepareElement(FormInterface $form) { if (true === $this->shouldCreateChildrenOnPrepareElement) { if ($this->targetElement !== null && $this->count > 0) { + $applyNames = count($this->keyNames) > 0 && count($this->keyNames) === $this->count ? 1 : 0; while ($this->count > $this->lastChildIndex + 1) { - $this->addNewTargetElementInstance(++$this->lastChildIndex); + $this->addNewTargetElementInstance($applyNames ? $this->keyNames[++$this->lastChildIndex] : ++$this->lastChildIndex); } } } From d8a283f0d570216e0045f258b6746211a2ff925f Mon Sep 17 00:00:00 2001 From: serus Date: Tue, 15 Dec 2015 09:07:23 +0100 Subject: [PATCH 2/2] label for each input ...'options' => ['key_names' => ['sk', 'de', 'en'],'labels' => ['SK', 'DE', 'EN']] --- src/Element/Collection.php | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/Element/Collection.php b/src/Element/Collection.php index 4c0d9d1f..65154811 100644 --- a/src/Element/Collection.php +++ b/src/Element/Collection.php @@ -150,6 +150,10 @@ public function setOptions($options) $this->setKeyNames(array_values($options['key_names'])); } + if (isset($options['labels']) && is_array($options['labels'])) { + $this->setLabels(array_values($options['labels'])); + } + return $this; } @@ -317,6 +321,28 @@ public function getCount() return $this->count; } + /** + * Set the labels for each item + * + * @param $labels + * @return Collection + */ + public function setLabels($labels) + { + $this->labels = $labels; + return $this; + } + + /** + * Get the labels + * + * @return int + */ + public function getLabels() + { + return $this->labels; + } + /** * Set the array key names * @@ -513,7 +539,8 @@ public function prepareElement(FormInterface $form) if ($this->targetElement !== null && $this->count > 0) { $applyNames = count($this->keyNames) > 0 && count($this->keyNames) === $this->count ? 1 : 0; while ($this->count > $this->lastChildIndex + 1) { - $this->addNewTargetElementInstance($applyNames ? $this->keyNames[++$this->lastChildIndex] : ++$this->lastChildIndex); + $label = isSet($this->labels[$this->lastChildIndex + 1]) ? $this->labels[$this->lastChildIndex + 1] : null; + $this->addNewTargetElementInstance($applyNames ? $this->keyNames[++$this->lastChildIndex] : ++$this->lastChildIndex, $label); } } } @@ -604,12 +631,13 @@ protected function createNewTargetElementInstance() * @return ElementInterface * @throws Exception\DomainException */ - protected function addNewTargetElementInstance($name) + protected function addNewTargetElementInstance($name, $label = null) { $this->shouldCreateChildrenOnPrepareElement = false; $elementOrFieldset = $this->createNewTargetElementInstance(); $elementOrFieldset->setName($name); + if($label) $elementOrFieldset->setLabel($label); $this->add($elementOrFieldset);