diff --git a/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php b/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php index b7f37137d8..4fa757295a 100644 --- a/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php +++ b/src/Symfony/Component/Form/ChoiceList/Factory/DefaultChoiceListFactory.php @@ -329,6 +329,8 @@ class DefaultChoiceListFactory implements ChoiceListFactoryInterface return; } + $groupLabel = (string) $groupLabel; + // Initialize the group views if necessary. Unnnecessarily built group // views will be cleaned up at the end of createView() if (!isset($preferredViews[$groupLabel])) { diff --git a/src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php index e5112bf38f..34f9991e0d 100644 --- a/src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php +++ b/src/Symfony/Component/Form/Tests/ChoiceList/Factory/DefaultChoiceListFactoryTest.php @@ -77,6 +77,13 @@ class DefaultChoiceListFactoryTest extends \PHPUnit_Framework_TestCase return $this->obj1 === $object || $this->obj2 === $object ? 'Group 1' : 'Group 2'; } + public function getGroupAsObject($object) + { + return $this->obj1 === $object || $this->obj2 === $object + ? new DefaultChoiceListFactoryTest_Castable('Group 1') + : new DefaultChoiceListFactoryTest_Castable('Group 2'); + } + protected function setUp() { $this->obj1 = (object) array('label' => 'A', 'index' => 'w', 'value' => 'a', 'preferred' => false, 'group' => 'Group 1', 'attr' => array()); @@ -581,6 +588,19 @@ class DefaultChoiceListFactoryTest extends \PHPUnit_Framework_TestCase $this->assertGroupedView($view); } + public function testCreateViewFlatGroupByObjectThatCanBeCastToString() + { + $view = $this->factory->createView( + $this->list, + array($this->obj2, $this->obj3), + null, // label + null, // index + array($this, 'getGroupAsObject') + ); + + $this->assertGroupedView($view); + } + public function testCreateViewFlatGroupByAsClosure() { $obj1 = $this->obj1; @@ -897,3 +917,18 @@ class DefaultChoiceListFactoryTest extends \PHPUnit_Framework_TestCase ), $view); } } + +class DefaultChoiceListFactoryTest_Castable +{ + private $property; + + public function __construct($property) + { + $this->property = $property; + } + + public function __toString() + { + return $this->property; + } +}