Skip to content

Commit

Permalink
fix: prevent two selected options, b/c apparently 'String' == 0 🤦
Browse files Browse the repository at this point in the history
There was a bug, where the options array ['Auto', 0, 1] would result in
HTML option tags where both the 'Auto' and the 0 option were selected.
  • Loading branch information
micgro42 committed Oct 26, 2017
1 parent 07ca0cc commit 389e185
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions _test/tests/inc/form/dropdownelement.test.php
Expand Up @@ -129,6 +129,21 @@ public function test_optgroups() {
$this->assertEquals('label of third option', $selected->text());
}

/**
* Prevent double select that might occur because `'Auto' == 0` is true
*/
public function test_doubleselect() {
$form = new Form\Form();
$form->addDropdown('foo', ['Auto', 0, 1]);

$html = $form->toHTML();

$pq = phpQuery::newDocumentXHTML($html);
$selected = $pq->find('option[selected=selected]');
$this->assertEquals(1, $selected->length);
$this->assertEquals('Auto', $selected->text());
}

/**
* Ensure that there is always only a single one selected option
*/
Expand Down
2 changes: 1 addition & 1 deletion inc/Form/OptGroup.php
Expand Up @@ -88,7 +88,7 @@ public function toHTML() {
protected function renderOptions() {
$html = '';
foreach($this->options as $key => $val) {
$selected = ($key == $this->value) ? ' selected="selected"' : '';
$selected = ((string)$key === (string)$this->value) ? ' selected="selected"' : '';
$attrs = '';
if (!empty($val['attrs']) && is_array($val['attrs'])) {
$attrs = buildAttributes($val['attrs']);
Expand Down

0 comments on commit 389e185

Please sign in to comment.