Skip to content

Commit e9849ef

Browse files
matej21dg
authored andcommitted
SelectBox, MultiSelectBox: added addOptionAttributes()
1 parent c94b94f commit e9849ef

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

src/Forms/Controls/MultiSelectBox.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class MultiSelectBox extends MultiChoiceControl
1818
/** @var array of option / optgroup */
1919
private $options = [];
2020

21+
/** @var array */
22+
private $optionAttributes = [];
23+
2124

2225
public function __construct($label = NULL, array $items = NULL)
2326
{
@@ -67,8 +70,18 @@ public function getControl()
6770
[
6871
'selected?' => $this->value,
6972
'disabled:' => is_array($this->disabled) ? $this->disabled : NULL,
70-
]
73+
] + $this->optionAttributes
7174
)->addAttributes(parent::getControl()->attrs)->multiple(TRUE);
7275
}
7376

77+
78+
/**
79+
* @return self
80+
*/
81+
public function addOptionAttributes(array $attributes)
82+
{
83+
$this->optionAttributes = $attributes + $this->optionAttributes;
84+
return $this;
85+
}
86+
7487
}

src/Forms/Controls/SelectBox.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class SelectBox extends ChoiceControl
2424
/** @var mixed */
2525
private $prompt = FALSE;
2626

27+
/** @var array */
28+
private $optionAttributes = [];
29+
2730

2831
public function __construct($label = NULL, array $items = NULL)
2932
{
@@ -96,11 +99,21 @@ public function getControl()
9699
[
97100
'selected?' => $this->value,
98101
'disabled:' => is_array($this->disabled) ? $this->disabled : NULL,
99-
]
102+
] + $this->optionAttributes
100103
)->addAttributes(parent::getControl()->attrs);
101104
}
102105

103106

107+
/**
108+
* @return self
109+
*/
110+
public function addOptionAttributes(array $attributes)
111+
{
112+
$this->optionAttributes = $attributes + $this->optionAttributes;
113+
return $this;
114+
}
115+
116+
104117
/**
105118
* @return bool
106119
*/

tests/Forms/Controls.MultiSelectBox.render.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,15 @@ test(function () { // rendering options
131131
$input->getControl();
132132
Assert::true($input->getOption('rendered'));
133133
});
134+
135+
136+
test(function () {
137+
$form = new Form;
138+
$input = $form->addMultiSelect('list', 'Label', [
139+
1 => 'First',
140+
2 => 'Second',
141+
])->setValue(1);
142+
$input->addOptionAttributes(['bar' => 'b', 'selected?' => 2, 'foo:' => [1 => 'a', 2 => 'b']]);
143+
$input->addOptionAttributes(['bar' => 'c']);
144+
Assert::same('<select name="list[]" id="frm-list" multiple><option bar="c" value="1" selected foo="a">First</option><option bar="c" value="2" foo="b">Second</option></select>', (string) $input->getControl());
145+
});

tests/Forms/Controls.SelectBox.render.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,15 @@ test(function () { // rendering options
131131
$input->getControl();
132132
Assert::true($input->getOption('rendered'));
133133
});
134+
135+
136+
test(function () {
137+
$form = new Form;
138+
$input = $form->addSelect('list', 'Label', [
139+
1 => 'First',
140+
2 => 'Second',
141+
])->setValue(1);
142+
$input->addOptionAttributes(['bar' => 'b', 'selected?' => 2, 'foo:' => [1 => 'a', 2 => 'b']]);
143+
$input->addOptionAttributes(['bar' => 'c']);
144+
Assert::same('<select name="list" id="frm-list"><option bar="c" value="1" selected foo="a">First</option><option bar="c" value="2" foo="b">Second</option></select>', (string) $input->getControl());
145+
});

0 commit comments

Comments
 (0)