Ver código fonte

Updated logic and added Exception messages when an incorrect configuration is used.

caponica 10 anos atrás
pai
commit
d3ce16f857

+ 15 - 5
Form/DataTransformer/ModelToIdPropertyTransformer.php

@@ -84,12 +84,22 @@ class ModelToIdPropertyTransformer implements DataTransformerInterface
         if (!$entityOrCollection) {
         if (!$entityOrCollection) {
             return $result;
             return $result;
         }
         }
-        if (substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
-            $collection = array($entityOrCollection);
-        } elseif ($entityOrCollection instanceof \ArrayAccess) {
-            $collection = $entityOrCollection;
+        if ($this->multiple) {
+            if (substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
+                throw new \InvalidArgumentException('A multiple selection must be passed a collection not a single value.');
+            } elseif ($entityOrCollection instanceof \ArrayAccess) {
+                $collection = $entityOrCollection;
+            } else {
+                throw new \InvalidArgumentException('A multiple selection must be passed a collection not a single value.');
+            }
         } else {
         } else {
-            $collection = array($entityOrCollection);
+            if (substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
+                $collection = array($entityOrCollection);
+            } elseif ($entityOrCollection instanceof \ArrayAccess) {
+                throw new \InvalidArgumentException('A single selection must be passed a single value not a collection.');
+            } else {
+                $collection = array($entityOrCollection);
+            }
         }
         }
 
 
         if (empty($this->property)) {
         if (empty($this->property)) {