Skip to content

Commit 720eb9d

Browse files
committed
Fix bug where selects with optgroups weren't populated correctly
1 parent a588c25 commit 720eb9d

File tree

4 files changed

+61
-12
lines changed

4 files changed

+61
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## CHANGELOG
22

3-
3.3.0 (develop)
3+
3.3.0 (stable)
44
-----
55

66
- [add] Add ability to pass attributes to a select's options
@@ -10,10 +10,11 @@
1010
- [add] Allow switching to alternate icon fonts
1111
- [mod] Form classes are now framework-dependant
1212
- [mod] More work on the Bootstrap 3 integration
13-
- [fix] Prevent custom groups from responding to errors fro non-grouped fields
13+
- [fix] Prevent custom groups from responding to errors from non-grouped fields
1414
- [fix] Fix bug in selection false values in Selects
15+
- [fix] Fix bug where selects with optgroups weren't populated correctly
1516

16-
3.2.0 (stable)
17+
3.2.0
1718
-----
1819

1920
- **[mod] Updated TwitterBootstrap3 to the latest release**

composer.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Former/Form/Fields/Select.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ public function render()
9191
// Mark selected values as selected
9292
if ($this->hasChildren() and !empty($this->value)) {
9393
foreach ($this->value as $value) {
94-
if ($child = $this->getChild($value)) {
95-
$child->selected('selected');
96-
}
94+
$this->selectValue($value);
9795
}
9896
}
9997

@@ -107,6 +105,39 @@ public function render()
107105
return parent::render();
108106
}
109107

108+
/**
109+
* Select a value in the field's children
110+
*
111+
* @param mixed $value
112+
* @param Element $parent
113+
*
114+
* @return void
115+
*/
116+
protected function selectValue($value, $parent = null)
117+
{
118+
// If no parent element defined, use direct children
119+
if (!$parent) {
120+
$parent = $this;
121+
}
122+
123+
// Look in direct childs
124+
if ($child = $parent->getChild($value)) {
125+
return $child->selected('selected');
126+
}
127+
128+
foreach ($parent->getChildren() as $child) {
129+
// Search by value
130+
if ($child->getAttribute('value') == $value) {
131+
$child->selected('selected');
132+
}
133+
134+
// Else iterate over subchilds
135+
if ($child->hasChildren()) {
136+
$this->selectValue($value, $child);
137+
}
138+
}
139+
}
140+
110141
/**
111142
* Get the Select's placeholder
112143
*

tests/Fields/SelectTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ public function testCanCreateRangeSelects()
278278

279279
public function testCanCreateSelectGroups()
280280
{
281-
$values = array('foo' => array(1 => 'foo', 2 => 'bar'), 'bar' => array(1 => 'foo', 2 => 'bar'));
281+
$values = array('foo' => array(1 => 'foo', 2 => 'bar'), 'bar' => array(3 => 'foo', 4 => 'bar'));
282282
$select = $this->former->select('foo')->options($values);
283283

284284
$matcher =
@@ -287,8 +287,25 @@ public function testCanCreateSelectGroups()
287287
'<option value="1">foo</option><option value="2">bar</option>'.
288288
'</optgroup>'.
289289
'<optgroup label="bar">'.
290+
'<option value="3">foo</option><option value="4">bar</option>'.
291+
'</optgroup>'.
292+
'</select>';
293+
$this->assertEquals($matcher, $select->render());
294+
}
295+
296+
public function testCanPopulateSelectGroups()
297+
{
298+
$values = array('foo' => array(1 => 'foo', 2 => 'bar'), 'bar' => array(3 => 'foo', 4 => 'bar'));
299+
$select = $this->former->select('foo')->options($values)->select(4);
300+
301+
$matcher =
302+
'<select id="foo" name="foo">'.
303+
'<optgroup label="foo">'.
290304
'<option value="1">foo</option><option value="2">bar</option>'.
291305
'</optgroup>'.
306+
'<optgroup label="bar">'.
307+
'<option value="3">foo</option><option value="4" selected="selected">bar</option>'.
308+
'</optgroup>'.
292309
'</select>';
293310
$this->assertEquals($matcher, $select->render());
294311
}

0 commit comments

Comments
 (0)