Skip to content

Commit

Permalink
Fixing FormHelper::__selectOptions incorrectly selecting options due …
Browse files Browse the repository at this point in the history
…to type juggling. Fixes #167
  • Loading branch information
markstory committed Oct 19, 2009
1 parent a36c2ec commit 2675bbc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cake/libs/view/helpers/form.php
Expand Up @@ -1721,7 +1721,7 @@ function __selectOptions($elements = array(), $selected = null, $parents = array
}

if ($name !== null) {
if ((!$selectedIsEmpty && $selected == $name) || ($selectedIsArray && in_array($name, $selected))) {
if ((!$selectedIsEmpty && (string)$selected == (string)$name) || ($selectedIsArray && in_array($name, $selected))) {
if ($attributes['style'] === 'checkbox') {
$htmlOptions['checked'] = true;
} else {
Expand Down
17 changes: 17 additions & 0 deletions cake/tests/cases/libs/view/helpers/form.test.php
Expand Up @@ -2687,6 +2687,23 @@ function testSelect() {
'/select'
);
$this->assertTags($result, $expected);

$this->Form->data = array('Model' => array('contact_id' => 228));
$result = $this->Form->select(
'Model.contact_id',
array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
null, array('escape' => false), 'pick something'
);

$expected = array(
'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
array('option' => array('value' => '')), 'pick something', '/option',
array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
array('option' => array('value' => '228-1')), '228-1 value', '/option',
array('option' => array('value' => '228-2')), '228-2 value', '/option',
'/select'
);
$this->assertTags($result, $expected);
}
/**
* Tests that FormHelper::select() allows null to be passed in the $attributes parameter
Expand Down

0 comments on commit 2675bbc

Please sign in to comment.