Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit cbc3b2f

Browse files
Erik van Velzenweierophinney
authored andcommitted
Fix rendering select element when value is an object
1 parent afa14de commit cbc3b2f

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ All notable changes to this project will be documented in this file, in reverse
2222

2323
### Fixed
2424

25+
- [#218](https://github.com/zendframework/zend-form/pull/218) ensures object values of select elements can be rendered without error.
26+
2527
- [#216](https://github.com/zendframework/zend-form/pull/216) fixes an issue when performing data binding and a fieldset has no mapped
2628
input elements, casting `null` values to empty arrays to ensure they can be
2729
passed to an input filter.

src/View/Helper/FormSelect.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ protected function validateMultiValue($value, array $attributes)
285285
}
286286

287287
if (! is_array($value)) {
288-
return (array) $value;
288+
return [$value];
289289
}
290290

291291
if (! isset($attributes['multiple']) || ! $attributes['multiple']) {

test/TestAsset/Identifier.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace ZendTest\Form\TestAsset;
11+
12+
class Identifier
13+
{
14+
private $id;
15+
16+
public function __construct($id)
17+
{
18+
$this->id = $id;
19+
}
20+
21+
public function __toString()
22+
{
23+
return (string) $this->id;
24+
}
25+
}

test/View/Helper/FormSelectTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Zend\Form\Element;
1313
use Zend\Form\Element\Select as SelectElement;
1414
use Zend\Form\View\Helper\FormSelect as FormSelectHelper;
15+
use ZendTest\Form\TestAsset\Identifier;
1516

1617
class FormSelectTest extends CommonTestCase
1718
{
@@ -414,4 +415,31 @@ public function testRenderElementWithNoNameRaisesException()
414415
$this->expectException('Zend\Form\Exception\DomainException');
415416
$this->helper->render($element);
416417
}
418+
419+
public function getElementWithObjectIdentifiers()
420+
{
421+
$element = new SelectElement('foo');
422+
$options = [
423+
[
424+
'label' => 'This is the first label',
425+
'value' => new Identifier(42),
426+
],
427+
[
428+
'label' => 'This is the second label',
429+
'value' => new Identifier(43),
430+
],
431+
];
432+
$element->setValueOptions($options);
433+
return $element;
434+
}
435+
436+
public function testRenderElementWithObjectIdentifiers()
437+
{
438+
$element = $this->getElementWithObjectIdentifiers();
439+
$element->setValue(new Identifier(42));
440+
441+
$markup = $this->helper->render($element);
442+
$this->assertRegexp('#option .*?value="42" selected="selected"#', $markup);
443+
$this->assertNotRegexp('#option .*?value="43" selected="selected"#', $markup);
444+
}
417445
}

0 commit comments

Comments
 (0)