Skip to content

Commit

Permalink
Allowing zero-valued options and the empty param to play nice with ea…
Browse files Browse the repository at this point in the history
…ch other in the select form helper.
  • Loading branch information
toomuchpete committed Apr 11, 2012
1 parent 26ef775 commit 4b32e14
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
3 changes: 2 additions & 1 deletion template/helper/Form.php
Expand Up @@ -558,7 +558,8 @@ protected function _selectOptions(array $list, array $scope) {
}
$selected = (
(is_array($scope['value']) && in_array($value, $scope['value'])) ||
($scope['value'] == $value)
($scope['empty'] && empty($scope['value']) && $value === '') ||
($scope['value'] === (is_integer($value) ? (string) $value : $value) )
);
$options = $selected ? array('selected' => true) : array();
$params = compact('value', 'title', 'options');
Expand Down
12 changes: 9 additions & 3 deletions tests/cases/template/helper/FormTest.php
Expand Up @@ -602,14 +602,17 @@ public function testSelectGeneration() {
}

public function testSelectWithEmptyOption() {
$result = $this->form->select('numbers', array('1' => 'first', '2' => 'second'), array(
$result = $this->form->select('numbers', array('zero', 'first', 'second'), array(
'empty' => true
));

$this->assertTags($result, array(
'select' => array('id' => 'Numbers', 'name' => 'numbers'),
array('option' => array('value' => '', 'selected' => 'selected')),
'/option',
array('option' => array('value' => '0')),
'zero',
'/option',
array('option' => array('value' => '1')),
'first',
'/option',
Expand All @@ -619,7 +622,7 @@ public function testSelectWithEmptyOption() {
'/select'
));

$result = $this->form->select('numbers', array('1' => 'first', '2' => 'second'), array(
$result = $this->form->select('numbers', array('zero', 'first', 'second'), array(
'empty' => '> Make a selection'
));

Expand All @@ -628,6 +631,9 @@ public function testSelectWithEmptyOption() {
array('option' => array('value' => '', 'selected' => 'selected')),
'> Make a selection',
'/option',
array('option' => array('value' => '0')),
'zero',
'/option',
array('option' => array('value' => '1')),
'first',
'/option',
Expand Down Expand Up @@ -960,7 +966,7 @@ public function testFormFieldSelect() {
'div' => array(),
'label' => array('for' => 'States'), 'States', '/label',
'select' => array('name' => 'states', 'id' => 'States'),
array('option' => array('value' => '0', 'selected' => 'selected')),
array('option' => array('value' => '0')),
'CA',
'/option',
array('option' => array('value' => '1')),
Expand Down

0 comments on commit 4b32e14

Please sign in to comment.