Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add unsetValueOption() to the Form\Element\Select and Form\Element\MultiCheckbox #5562

Merged
merged 2 commits into from

4 participants

@bacinsky

It could be nice to have such a method to remove a value option from a form element in runtime.

Instead of this in the user land:

$valueOptions = $element->getValueOptions();
if (isset($valueOptions['something'])) {
    unset($valueOptions['something']);
    $element->setValueOptions($valueOptions);
}

just this:

$element->unsetValueOption('something');
library/Zend/Form/Element/MultiCheckbox.php
@@ -72,6 +72,19 @@ public function setValueOptions(array $options)
}
/**
+ * @param string $key
+ * @return Select

@return self

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
library/Zend/Form/Element/Select.php
@@ -85,6 +85,19 @@ public function setValueOptions(array $options)
}
/**
+ * @param string $key
+ * @return Select

@return self

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Maks3w
Collaborator

@bacinsky Please send a PR to zf2-documentation too

@weierophinney weierophinney added this to the 2.3.0 milestone
@weierophinney weierophinney self-assigned this
@weierophinney weierophinney merged commit 1852f28 into from
@bacinsky bacinsky deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
13 library/Zend/Form/Element/MultiCheckbox.php
@@ -72,6 +72,19 @@ public function setValueOptions(array $options)
}
/**
+ * @param string $key
+ * @return self
+ */
+ public function unsetValueOption($key)
+ {
+ if (isset($this->valueOptions[$key])) {
+ unset($this->valueOptions[$key]);
+ }
+
+ return $this;
+ }
+
+ /**
* Set options for an element. Accepted options are:
* - label: label to associate with the element
* - label_attributes: attributes to use when the label is rendered
View
13 library/Zend/Form/Element/Select.php
@@ -85,6 +85,19 @@ public function setValueOptions(array $options)
}
/**
+ * @param string $key
+ * @return self
+ */
+ public function unsetValueOption($key)
+ {
+ if (isset($this->valueOptions[$key])) {
+ unset($this->valueOptions[$key]);
+ }
+
+ return $this;
+ }
+
+ /**
* Set options for an element. Accepted options are:
* - label: label to associate with the element
* - label_attributes: attributes to use when the label is rendered
View
28 tests/ZendTest/Form/Element/MultiCheckboxTest.php
@@ -145,4 +145,32 @@ public function testDisableInputSpecification()
$inputSpec = $element->getInputSpecification();
$this->assertArrayNotHasKey('validators', $inputSpec);
}
+
+ public function testUnsetValueOption()
+ {
+ $element = new MultiCheckboxElement();
+ $element->setValueOptions(array(
+ 'Option 1' => 'option1',
+ 'Option 2' => 'option2',
+ 'Option 3' => 'option3',
+ ));
+ $element->unsetValueOption('Option 2');
+
+ $valueOptions = $element->getValueOptions();
+ $this->assertArrayNotHasKey('Option 2', $valueOptions);
+ }
+
+ public function testUnsetUndefinedValueOption()
+ {
+ $element = new MultiCheckboxElement();
+ $element->setValueOptions(array(
+ 'Option 1' => 'option1',
+ 'Option 2' => 'option2',
+ 'Option 3' => 'option3',
+ ));
+ $element->unsetValueOption('Option Undefined');
+
+ $valueOptions = $element->getValueOptions();
+ $this->assertArrayNotHasKey('Option Undefined', $valueOptions);
+ }
}
View
28 tests/ZendTest/Form/Element/SelectTest.php
@@ -207,4 +207,32 @@ public function testDisableInputSpecification()
$this->assertArrayNotHasKey('validators', $inputSpec);
}
+ public function testUnsetValueOption()
+ {
+ $element = new SelectElement();
+ $element->setValueOptions(array(
+ 'Option 1' => 'option1',
+ 'Option 2' => 'option2',
+ 'Option 3' => 'option3',
+ ));
+ $element->unsetValueOption('Option 2');
+
+ $valueOptions = $element->getValueOptions();
+ $this->assertArrayNotHasKey('Option 2', $valueOptions);
+ }
+
+ public function testUnsetUndefinedValueOption()
+ {
+ $element = new SelectElement();
+ $element->setValueOptions(array(
+ 'Option 1' => 'option1',
+ 'Option 2' => 'option2',
+ 'Option 3' => 'option3',
+ ));
+ $element->unsetValueOption('Option Undefined');
+
+ $valueOptions = $element->getValueOptions();
+ $this->assertArrayNotHasKey('Option Undefined', $valueOptions);
+ }
+
}
Something went wrong with that request. Please try again.