Skip to content
Browse files

Merge branch 'form/create-empty-option' of github.com:bakura10/zf2 in…

…to form/create-empty-option
  • Loading branch information...
2 parents bcc6ee9 + 7cf2cb1 commit 7a86ea574943d9d8512c9f24a5682ddd25dfe9ba @bakura10 committed Sep 10, 2012
View
34 library/Zend/Form/Element/Select.php
@@ -52,6 +52,13 @@ class Select extends Element implements InputProviderInterface
protected $validator;
/**
+ * Create an empty option (option with label but no value). If set to null, no option is created
+ *
+ * @var bool
+ */
+ protected $emptyOption = null;
+
+ /**
* @var array
*/
protected $valueOptions = array();
@@ -79,6 +86,7 @@ public function setValueOptions(array $options)
* - label: label to associate with the element
* - label_attributes: attributes to use when the label is rendered
* - value_options: list of values and labels for the select options
+ * _ create_empty_option: should an empty option be prepended to the options ?
*
* @param array|\Traversable $options
* @return Select|ElementInterface
@@ -96,6 +104,10 @@ public function setOptions($options)
$this->setValueOptions($this->options['options']);
}
+ if (isset($this->options['create_empty_option'])) {
+ $this->setShouldCreateEmptyOption($this->options['create_empty_option']);
+ }
+
return $this;
}
@@ -118,6 +130,28 @@ public function setAttribute($key, $value)
}
/**
+ * Set the string for an empty option (can be empty string). If set to null, no option will be added
+ *
+ * @param string|null $emptyOption
+ * @return Select
+ */
+ public function setEmptyOption($emptyOption)
+ {
+ $this->emptyOption = $emptyOption;
+ return $this;
+ }
+
+ /**
+ * Return the string for the empty option (null if none)
+ *
+ * @return string|null
+ */
+ public function getEmptyOption()
+ {
+ return $this->emptyOption;
+ }
+
+ /**
* Get validator
*
* @return ValidatorInterface
View
5 library/Zend/Form/View/Helper/FormSelect.php
@@ -76,7 +76,6 @@ public function render(ElementInterface $element)
));
}
-
$options = $element->getValueOptions();
if (empty($options)) {
throw new Exception\DomainException(sprintf(
@@ -85,6 +84,10 @@ public function render(ElementInterface $element)
));
}
+ if (($emptyOption = $element->getEmptyOption()) !== null) {
+ $options = array('' => $emptyOption) + $options;
+ }
+
$attributes = $element->getAttributes();
$value = $this->validateMultiValue($element->getValue(), $attributes);
View
2 tests/ZendTest/Form/Element/SelectTest.php
@@ -137,4 +137,6 @@ public function testDeprecateOptionsInAttributes()
));
$this->assertEquals($valueOptions, $element->getValueOptions());
}
+
+
}
View
59 tests/ZendTest/Form/View/Helper/FormSelectTest.php
@@ -251,4 +251,63 @@ public function testDoesNotThrowExceptionIfNameIsZero()
$markup = $this->helper->__invoke($element);
$this->assertContains('name="0"', $markup);
}
+
+ public function testCanCreateEmptyOption()
+ {
+ $element = new SelectElement('foo');
+ $element->setEmptyOption('empty');
+ $element->setValueOptions(array(
+ array(
+ 'label' => 'label1',
+ 'value' => 'value1',
+ ),
+ ));
+ $markup = $this->helper->render($element);
+
+ $this->assertContains('<option value="">empty</option>', $markup);
+ }
+
+ public function testCanCreateEmptyOptionWithEmptyString()
+ {
+ $element = new SelectElement('foo');
+ $element->setEmptyOption('');
+ $element->setValueOptions(array(
+ array(
+ 'label' => 'label1',
+ 'value' => 'value1',
+ ),
+ ));
+ $markup = $this->helper->render($element);
+
+ $this->assertContains('<option value=""></option>', $markup);
+ }
+
+ public function testDoesNotRenderEmptyOptionByDefault()
+ {
+ $element = new SelectElement('foo');
+ $element->setValueOptions(array(
+ array(
+ 'label' => 'label1',
+ 'value' => 'value1',
+ ),
+ ));
+ $markup = $this->helper->render($element);
+
+ $this->assertNotContains('<option value=""></option>', $markup);
+ }
+
+ public function testNullEmptyOptionDoesNotRenderEmptyOption()
+ {
+ $element = new SelectElement('foo');
+ $element->setEmptyOption(null);
+ $element->setValueOptions(array(
+ array(
+ 'label' => 'label1',
+ 'value' => 'value1',
+ ),
+ ));
+ $markup = $this->helper->render($element);
+
+ $this->assertNotContains('<option value=""></option>', $markup);
+ }
}

0 comments on commit 7a86ea5

Please sign in to comment.
Something went wrong with that request. Please try again.