Skip to content

Commit

Permalink
Merge pull request phalcon#10790 from theDisco/2.0.x
Browse files Browse the repository at this point in the history
Correctly assing keys and values to select field options
  • Loading branch information
andresgutierrez committed Aug 17, 2015
2 parents 571b20f + 5b978d0 commit b6f0e26
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
9 changes: 8 additions & 1 deletion phalcon/forms/element/select.zep
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@ class Select extends Element implements ElementInterface {
*/
public function addOption(var option) -> <Element>
{
let this->_optionsValues[] = option;
if is_array(option) {
var key, value;
for key, value in option {
let this->_optionsValues[key] = value;
}
} else {
let this->_optionsValues[] = option;
}
return this;
}

Expand Down
16 changes: 16 additions & 0 deletions unit-tests/FormsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,4 +517,20 @@ public function testIssue2045()

$this->assertEquals('<input type="text" name="name" class="big-input" />', $element->render());
}

public function testCorrectlyAddOptionToSelectElementIfParameterIsAnArray()
{
$element = new Select('test-select');
$element->addOption(array('key' => 'value'));

$this->assertEquals('<select id="test-select" name="test-select"><option value="key">value</option></select>', preg_replace('/[[:cntrl:]]/', '', $element->render()));
}

public function testCorrectlyAddOptionToSelectElementIfParameterIsAString()
{
$element = new Select('test-select');
$element->addOption('value');

$this->assertEquals('<select id="test-select" name="test-select"><option value="0">value</option></select>', preg_replace('/[[:cntrl:]]/', '', $element->render()));
}
}

0 comments on commit b6f0e26

Please sign in to comment.