diff --git a/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php b/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php index 6a6eb0fd3d..a2b61b0ca7 100644 --- a/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php +++ b/src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php @@ -185,7 +185,7 @@ class EntityChoiceList extends ArrayChoiceList } else { // Otherwise expect a __toString() method in the entity if (!method_exists($entity, '__toString')) { - throw new FormException('Entities passed to the choice field must have a "__toString()" method defined (or you can also override the "property" option).'); + throw new FormException(sprintf('Entity "%s" passed to the choice field must have a "__toString()" method defined (or you can also override the "property" option).', $this->class)); } $value = (string) $entity; diff --git a/tests/Symfony/Tests/Bridge/Doctrine/Form/ChoiceList/EntityChoiceListTest.php b/tests/Symfony/Tests/Bridge/Doctrine/Form/ChoiceList/EntityChoiceListTest.php index 04ecbe4660..c263c124cd 100644 --- a/tests/Symfony/Tests/Bridge/Doctrine/Form/ChoiceList/EntityChoiceListTest.php +++ b/tests/Symfony/Tests/Bridge/Doctrine/Form/ChoiceList/EntityChoiceListTest.php @@ -40,6 +40,33 @@ class EntityChoiceListTest extends DoctrineOrmTestCase $this->em = null; } + /** + * @expectedException Symfony\Component\Form\Exception\FormException + * @expectedMessage Entity "Symfony\Tests\Bridge\Doctrine\Fixtures\SingleIdentEntity" passed to the choice field must have a "__toString()" method defined (or you can also override the "property" option). + */ + public function testEntitesMustHaveAToStringMethod() + { + $entity1 = new SingleIdentEntity(1, 'Foo'); + $entity2 = new SingleIdentEntity(2, 'Bar'); + + // Persist for managed state + $this->em->persist($entity1); + $this->em->persist($entity2); + + $choiceList = new EntityChoiceList( + $this->em, + self::SINGLE_IDENT_CLASS, + null, + null, + array( + $entity1, + $entity2, + ) + ); + + $choiceList->getEntities(); + } + /** * @expectedException Symfony\Component\Form\Exception\FormException */