Skip to content

Commit 23be8ee

Browse files
committed
Merge pull request #4 from simara-esports/master
Forms: Selectbox allow item with null caption [ID => null]
2 parents 27f81f5 + 349cad9 commit 23be8ee

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/Forms/Controls/ChoiceControl.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function loadHttpData()
5858
*/
5959
public function setValue($value)
6060
{
61-
if ($value !== NULL && !isset($this->items[(string) $value])) {
61+
if ($value !== NULL && !array_key_exists((string) $value, $this->items)) {
6262
$range = Nette\Utils\Strings::truncate(implode(', ', array_map(function($s) { return var_export($s, TRUE); }, $this->items)), 70, '...');
6363
throw new Nette\InvalidArgumentException("Value '$value' is out of allowed range [$range] in field '{$this->name}'.");
6464
}
@@ -73,7 +73,7 @@ public function setValue($value)
7373
*/
7474
public function getValue()
7575
{
76-
return isset($this->items[$this->value]) ? $this->value : NULL;
76+
return array_key_exists($this->value, $this->items) ? $this->value : NULL;
7777
}
7878

7979

tests/Forms/Controls.ChoiceControl.loadData.phpt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,16 @@ test(function() use ($series) { // disabled one
188188

189189
Assert::null( $input->getValue() );
190190
});
191+
192+
test(function() {
193+
$_POST = array('select' => 1);
194+
195+
$form = new Form;
196+
$input = $form['select'] = new ChoiceControl(NULL);
197+
$input->setItems(array(
198+
1 => NULL,
199+
2 => 'Red dwarf'
200+
));
201+
202+
Assert::same( 1, $input->getValue() );
203+
});

tests/Forms/Controls.SelectBox.loadData.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,15 @@ test(function() use ($series) { // disabled one
266266

267267
Assert::null( $input->getValue() );
268268
});
269+
270+
test(function() {
271+
$_POST = array('select' => 1);
272+
273+
$form = new Form;
274+
$input = $form->addSelect('select', NULL, array(
275+
1 => NULL,
276+
2 => 'Red dwarf'
277+
));
278+
279+
Assert::same( 1, $input->getValue() );
280+
});

0 commit comments

Comments
 (0)