Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Allow replacing elements within a form collection #4884

Merged
merged 3 commits into from

2 participants

@radnan

Currently you can only remove elements by submitting fewer elements than is requested. This patch will allow you to replace elements i.e. remove an element and add another one in its place but with a different key (element name).

weierophinney and others added some commits
@weierophinney weierophinney Merge branch 'version/bump'
Bump to 2.2.3dev
26e400d
@radnan radnan allow replacing elements within a form collection
currently you can only remove elements by submitting fewer elements than is requested. this patch will allow you to replace elements i.e. remove an element and add another one.
9928ca0
@radnan radnan tests to rmove and replace form collection elements d8f28b4
@weierophinney weierophinney merged commit d8f28b4 into zendframework:develop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 24, 2013
  1. @weierophinney

    Merge branch 'version/bump'

    weierophinney authored
    Bump to 2.2.3dev
Commits on Jul 25, 2013
  1. @radnan

    allow replacing elements within a form collection

    radnan authored
    currently you can only remove elements by submitting fewer elements than is requested. this patch will allow you to replace elements i.e. remove an element and add another one.
  2. @radnan
This page is out of date. Refresh to see the latest.
View
30 library/Zend/Form/Element/Collection.php
@@ -187,25 +187,33 @@ public function populateValues($data)
return;
}
+ // If there is fewer data and allowRemove is true, we reset the element count
if (count($data) < $this->getCount()) {
if (!$this->allowRemove) {
throw new Exception\DomainException(sprintf(
'There are fewer elements than specified in the collection (%s). Either set the allow_remove option ' .
'to true, or re-submit the form.',
get_class($this)
- )
- );
+ ));
}
- // If there are less data and that allowRemove is true, we remove elements that are not presents
$this->setCount(count($data));
- foreach ($this->byName as $name => $elementOrFieldset) {
- if (isset($data[$name])) {
- continue;
- }
+ }
- $this->remove($name);
+ // Check to see if elements have been replaced or removed
+ foreach ($this->byName as $name => $elementOrFieldset) {
+ if (isset($data[$name])) {
+ continue;
+ }
+
+ if (!$this->allowRemove) {
+ throw new Exception\DomainException(sprintf(
+ 'Elements have been removed from the collection (%s) but the allow_remove option is not true.',
+ get_class($this)
+ ));
}
+
+ $this->remove($name);
}
if ($this->targetElement instanceof FieldsetInterface) {
@@ -217,8 +225,10 @@ public function populateValues($data)
}
} else {
foreach ($this->byName as $name => $element) {
- $element->setAttribute('value', $data[$name]);
- unset($data[$name]);
+ if (isset($data[$name])) {
+ $element->setAttribute('value', $data[$name]);
+ unset($data[$name]);
+ }
}
}
View
39 tests/ZendTest/Form/Element/CollectionTest.php
@@ -79,6 +79,45 @@ public function testCanAddNewElementsIfAllowAddIsTrue()
$this->assertEquals(3, count($collection->getElements()));
}
+ public function testCanRemoveElementsIfAllowRemoveIsTrue()
+ {
+ $collection = $this->form->get('colors');
+ $collection->setAllowRemove(true);
+ $this->assertTrue($collection->allowRemove());
+
+ $data = array();
+ $data[] = 'blue';
+ $data[] = 'green';
+
+ $collection->populateValues($data);
+ $this->assertEquals(2, count($collection->getElements()));
+
+ unset($data[0]);
+
+ $collection->populateValues($data);
+ $this->assertEquals(1, count($collection->getElements()));
+ }
+
+ public function testCanReplaceElementsIfAllowAddAndAllowRemoveIsTrue()
+ {
+ $collection = $this->form->get('colors');
+ $collection->setAllowAdd(true);
+ $collection->setAllowRemove(true);
+
+ $data = array();
+ $data[] = 'blue';
+ $data[] = 'green';
+
+ $collection->populateValues($data);
+ $this->assertEquals(2, count($collection->getElements()));
+
+ unset($data[0]);
+ $data[] = 'orange';
+
+ $collection->populateValues($data);
+ $this->assertEquals(2, count($collection->getElements()));
+ }
+
public function testCanValidateFormWithCollectionWithoutTemplate()
{
$this->form->setData(array(
Something went wrong with that request. Please try again.