Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
Merge branch 'feature/3509' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Jan 22, 2013
2 parents 2adf713 + d0e18c2 commit dcc3c04
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
3 changes: 2 additions & 1 deletion library/Zend/Form/View/Helper/FormSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Zend\Form\ElementInterface;
use Zend\Form\Element\Select as SelectElement;
use Zend\Form\Exception;
use Zend\Stdlib\ArrayUtils;

class FormSelect extends AbstractHelper
{
Expand Down Expand Up @@ -148,7 +149,7 @@ public function renderOptions(array $options, array $selectedOptions = array())
$disabled = $optionSpec['disabled'];
}

if (in_array($value, $selectedOptions)) {
if (ArrayUtils::inArray($value, $selectedOptions)) {
$selected = true;
}

Expand Down
30 changes: 30 additions & 0 deletions library/Zend/Stdlib/ArrayUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,36 @@ public static function isHashTable($value, $allowEmpty = false)
return (array_values($value) !== $value);
}

/**
* Checks if a value exists in an array.
*
* Due to "foo" == 0 === TRUE with in_array when strict = false, an option
* has been added to prevent this. When $strict = 0/false, the most secure
* non-strict check is implemented. if $strict = -1, the default in_array
* non-strict behaviour is used.
*
* @param mixed $needle
* @param array $haystack
* @param int|bool $strict
* @return bool
*/
public static function inArray($needle, array $haystack, $strict = false)
{
if (!$strict) {
if (is_int($needle) || is_float($needle)) {
$needle = (string) $needle;
}
if (is_string($needle)) {
foreach ($haystack as &$h) {
if (is_int($h) || is_float($h)) {
$h = (string) $h;
}
}
}
}
return in_array($needle, $haystack, $strict);
}

/**
* Convert an iterator to an array.
*
Expand Down
20 changes: 20 additions & 0 deletions tests/ZendTest/Form/View/Helper/FormSelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,26 @@ public function testNullEmptyOptionDoesNotRenderEmptyOption()
$this->assertNotContains('<option value=""></option>', $markup);
}

public function testCanMarkOptionsAsSelectedWhenEmptyOptionOrZeroValueSelected()
{
$element = new SelectElement('foo');
$element->setEmptyOption('empty');
$element->setValueOptions(array(
0 => 'label0',
1 => 'label1',
));

$element->setValue('');
$markup = $this->helper->render($element);
$this->assertContains('<option value="" selected="selected">empty</option>', $markup);
$this->assertContains('<option value="0">label0</option>', $markup);

$element->setValue('0');
$markup = $this->helper->render($element);
$this->assertContains('<option value="">empty</option>', $markup);
$this->assertContains('<option value="0" selected="selected">label0</option>', $markup);
}

public function testRenderInputNotSelectElementRaisesException()
{
$element = new Element\Text('foo');
Expand Down

0 comments on commit dcc3c04

Please sign in to comment.