Browse Source

Merge pull request #1877 from lvarayut/master

Specify an error massage by adding throw StringCastException
Thomas 11 năm trước cách đây
mục cha
commit
34e322920e
1 tập tin đã thay đổi với 7 bổ sung2 xóa
  1. 7 2
      Form/ChoiceList/ModelChoiceList.php

+ 7 - 2
Form/ChoiceList/ModelChoiceList.php

@@ -15,6 +15,7 @@ use Doctrine\Common\Util\ClassUtils;
 use Symfony\Component\PropertyAccess\PropertyAccess;
 use Symfony\Component\PropertyAccess\PropertyPath;
 use Symfony\Component\Form\Exception\InvalidArgumentException;
+use Symfony\Component\Form\Exception\RuntimeException;
 use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList;
 use Sonata\AdminBundle\Model\ModelManagerInterface;
 
@@ -136,8 +137,12 @@ class ModelChoiceList extends SimpleChoiceList
                 $propertyAccessor = PropertyAccess::getPropertyAccessor();
                 $value = $propertyAccessor->getValue($entity, $this->propertyPath);
             } else {
-                // Otherwise expect a __toString() method in the entity
-                $value = (string) $entity;
+                 // Otherwise expect a __toString() method in the entity
+                try {
+                    $value = (string) $entity;
+                } catch (\Exception $e) {
+                    throw new RuntimeException(sprintf("Unable to convert the entity %s to String, entity must have a '__toString()' method defined", ClassUtils::getClass($entity)), 0, $e);
+                }
             }
 
             if (count($this->identifier) > 1) {