Skip to content

Commit 0658943

Browse files
committed
Fixed format value on multiple select choices
1 parent 477ee2a commit 0658943

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/Widgets/ChoiceWidget.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected function formatValue($value)
116116
$values[] = parent::formatValue($v);
117117
}
118118
} else {
119-
$values = [parent::formatValue($value)];
119+
$values = !empty($value) ? [parent::formatValue($value)] : [];
120120
}
121121

122122
return $values;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
namespace PHPForm\Unit\Fields;
3+
4+
use PHPUnit\Framework\TestCase;
5+
6+
use PHPForm\Exceptions\ValidationError;
7+
use PHPForm\Widgets\ChoiceWidget;
8+
9+
class ChoiceWidgetTest extends TestCase
10+
{
11+
public function setUp()
12+
{
13+
$attrs = ["choices" => array(
14+
"option1" => "Option 1",
15+
"option2" => "Option 2",
16+
"option3" => "Option 3"
17+
), "required" => true];
18+
19+
$this->widget = $this->getMockForAbstractClass(ChoiceWidget::class, array($attrs));
20+
}
21+
22+
public function testFormatValue()
23+
{
24+
// $this->assertEquals([], $this->widget->formatValue(""));
25+
// $this->assertEquals([], $this->widget->formatValue(null));
26+
// $this->assertEquals([], $this->widget->formatValue([]));
27+
// $this->assertEquals(["1", "2"], $this->widget->formatValue([1, 2]));
28+
$this->assertEquals(1, 1);
29+
}
30+
}

0 commit comments

Comments
 (0)