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

Fix rendering select element when value is an object #218

Merged
merged 1 commit into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- [#218](https://github.com/zendframework/zend-form/pull/218) ensures object values of select elements can be rendered without error.

- [#216](https://github.com/zendframework/zend-form/pull/216) fixes an issue when performing data binding and a fieldset has no mapped
input elements, casting `null` values to empty arrays to ensure they can be
passed to an input filter.
Expand Down
2 changes: 1 addition & 1 deletion src/View/Helper/FormSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ protected function validateMultiValue($value, array $attributes)
}

if (! is_array($value)) {
return (array) $value;
return [$value];
Copy link
Contributor Author

@Erikvv Erikvv Aug 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The actual fix.

This line worked for string and int, creating an array with 1 element.

But for an object it will make an array of its properties and later on it will be treated like multiple values as if it's a multiselect.

}

if (! isset($attributes['multiple']) || ! $attributes['multiple']) {
Expand Down
25 changes: 25 additions & 0 deletions test/TestAsset/Identifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Form\TestAsset;

class Identifier
{
private $id;

public function __construct($id)
{
$this->id = $id;
}

public function __toString()
{
return (string) $this->id;
}
}
28 changes: 28 additions & 0 deletions test/View/Helper/FormSelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Zend\Form\Element;
use Zend\Form\Element\Select as SelectElement;
use Zend\Form\View\Helper\FormSelect as FormSelectHelper;
use ZendTest\Form\TestAsset\Identifier;

class FormSelectTest extends CommonTestCase
{
Expand Down Expand Up @@ -414,4 +415,31 @@ public function testRenderElementWithNoNameRaisesException()
$this->expectException('Zend\Form\Exception\DomainException');
$this->helper->render($element);
}

public function getElementWithObjectIdentifiers()
{
$element = new SelectElement('foo');
$options = [
[
'label' => 'This is the first label',
'value' => new Identifier(42),
],
[
'label' => 'This is the second label',
'value' => new Identifier(43),
],
];
$element->setValueOptions($options);
return $element;
}

public function testRenderElementWithObjectIdentifiers()
{
$element = $this->getElementWithObjectIdentifiers();
$element->setValue(new Identifier(42));

$markup = $this->helper->render($element);
$this->assertRegexp('#option .*?value="42" selected="selected"#', $markup);
$this->assertNotRegexp('#option .*?value="43" selected="selected"#', $markup);
}
}