Skip to content

Commit

Permalink
Merge branch 'form/create-empty-option' of github.com:bakura10/zf2 in…
Browse files Browse the repository at this point in the history
…to form/create-empty-option
  • Loading branch information
bakura10 committed Sep 10, 2012
2 parents bcc6ee9 + 7cf2cb1 commit 7a86ea5
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
34 changes: 34 additions & 0 deletions library/Zend/Form/Element/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,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
*/
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

Expand All @@ -117,6 +129,28 @@ public function setAttribute($key, $value)
return parent::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
*
Expand Down
5 changes: 4 additions & 1 deletion library/Zend/Form/View/Helper/FormSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public function render(ElementInterface $element)
));
}


$options = $element->getValueOptions();
if (empty($options)) {
throw new Exception\DomainException(sprintf(
Expand All @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions tests/ZendTest/Form/Element/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,6 @@ public function testDeprecateOptionsInAttributes()
));
$this->assertEquals($valueOptions, $element->getValueOptions());
}


}
59 changes: 59 additions & 0 deletions tests/ZendTest/Form/View/Helper/FormSelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.