Skip to content

Commit a518fc7

Browse files
committed
Added CheckboxSelectMultiple Widget
1 parent 8381d3d commit a518fc7

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Abstract class for Widgets
4+
*/
5+
namespace PHPForm\Widgets;
6+
7+
use PHPForm\Widgets\Input;
8+
9+
class CheckboxSelectMultiple extends ChoiceWidget
10+
{
11+
const TEMPLATE = '<div>{options}</div>';
12+
const TEMPLATE_CHOICE = '<label for="{for}">' . Input::TEMPLATE . ' {label}</label>';
13+
14+
protected $allow_multiple_selected = true;
15+
protected $option_inherits_attrs = true;
16+
protected $selected_attribute = "checked";
17+
protected $input_type = "checkbox";
18+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
namespace PHPForm\Unit\Widgets;
3+
4+
use PHPUnit\Framework\TestCase;
5+
6+
use PHPForm\Widgets\CheckboxSelectMultiple;
7+
8+
class CheckboxSelectMultipleTest extends TestCase
9+
{
10+
public function setUp()
11+
{
12+
$this->widget = new CheckboxSelectMultiple(["option1" => "option1", "option2" => "option2"]);
13+
}
14+
15+
public function testRender()
16+
{
17+
$expected =
18+
'<div>' .
19+
'<label for="id_name_1">' .
20+
'<input id="id_name_1" name="name" type="checkbox" value="option1"/> option1' .
21+
'</label>' .
22+
'<label for="id_name_2">' .
23+
'<input id="id_name_2" name="name" type="checkbox" value="option2"/> option2'.
24+
'</label>' .
25+
'</div>';
26+
27+
$this->assertXmlStringEqualsXmlString($expected, $this->widget->render("name", null));
28+
}
29+
30+
public function testRenderChecked()
31+
{
32+
$expected =
33+
'<div>' .
34+
'<label for="id_name_1">' .
35+
'<input id="id_name_1" name="name" type="checkbox" checked="checked" value="option1"/> option1' .
36+
'</label>' .
37+
'<label for="id_name_2">' .
38+
'<input id="id_name_2" name="name" type="checkbox" checked="checked" value="option2"/> option2'.
39+
'</label>' .
40+
'</div>';
41+
42+
$this->assertXmlStringEqualsXmlString($expected, $this->widget->render("name", ["option1", "option2"]));
43+
}
44+
}

0 commit comments

Comments
 (0)