Quellcode durchsuchen

Fix #171 - UploadedFile could not be converted to boolean error

Thomas Rabaix vor 14 Jahren
Ursprung
Commit
03ec3fbb4f

+ 5 - 1
Admin/AdminHelper.php

@@ -76,7 +76,11 @@ class AdminHelper
         // retrieve the FieldDescription
         $fieldDescription = $admin->getFormFieldDescription($childFormBuilder->getName());
 
-        $value = $fieldDescription->getValue($form->getData());
+        try {
+            $value = $fieldDescription->getValue($form->getData());
+        } catch (NoValueException $e) {
+            $value = null;
+        }
 
         // retrieve the posted data
         $data = $admin->getRequest()->get($formBuilder->getName());

+ 1 - 1
Admin/BaseFieldDescription.php

@@ -356,7 +356,7 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface
             }
         }
 
-        return false;
+        throw new NoValueException();
     }
 
     /**

+ 17 - 0
Admin/NoValueException.php

@@ -0,0 +1,17 @@
+<?php
+
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) 2010-2011 Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\AdminBundle\Admin;
+
+class NoValueException extends \Exception
+{
+
+}

+ 7 - 1
Builder/ORM/FormContractor.php

@@ -18,6 +18,7 @@ use Sonata\AdminBundle\Model\ModelManagerInterface;
 use Sonata\AdminBundle\Builder\FormContractorInterface;
 use Sonata\AdminBundle\Form\Type\AdminType;
 use Sonata\AdminBundle\Form\Type\ModelType;
+use Sonata\AdminBundle\Admin\NoValueException;
 
 use Symfony\Component\Form\FormBuilder;
 use Symfony\Component\Form\FormInterface;
@@ -229,7 +230,12 @@ class FormContractor implements FormContractorInterface
         // There is a bug in the GraphWalker, so for now we always load related associations
         // for more information : https://github.com/symfony/symfony/pull/1056
         if ($formBuilder->getData() && in_array($fieldDescription->getType(), array(ClassMetadataInfo::ONE_TO_MANY, ClassMetadataInfo::MANY_TO_MANY, ClassMetadataInfo::MANY_TO_ONE, ClassMetadataInfo::ONE_TO_ONE ))) {
-            $value = $fieldDescription->getValue($formBuilder->getData());
+            try {
+                $value = $fieldDescription->getValue($formBuilder->getData());
+            } catch (NoValueException $e) {
+                $value = null;
+            }
+
             $infos = $fieldDescription->getAssociationMapping();
             if ($value instanceof $infos['targetEntity'] && $value instanceof \Doctrine\ORM\Proxy\Proxy) {
                 $relatedId = 'get'.current($fieldDescription->getAdmin()->getModelManager()->getIdentifierFieldNames($infos['targetEntity']));

+ 3 - 3
Model/ORM/ModelManager.php

@@ -100,10 +100,10 @@ class ModelManager implements ModelManagerInterface
 
     /**
      * Find one object from the given class repository.
-     * 
+     *
      * @param string $class Class name
      * @param string|int $id Identifier. Can be a string with several IDs concatenated, separated by '-'.
-     * @return Object 
+     * @return Object
      */
     public function findOne($class, $id)
     {
@@ -190,7 +190,7 @@ class ModelManager implements ModelManagerInterface
     public function getNormalizedIdentifier($entity)
     {
         // the entities is not managed
-        if (!$this->getEntityManager()->getUnitOfWork()->isInIdentityMap($entity)) {
+        if (!$entity || !$this->getEntityManager()->getUnitOfWork()->isInIdentityMap($entity)) {
             return null;
         }
 

+ 16 - 8
Twig/Extension/SonataAdminExtension.php

@@ -13,6 +13,8 @@ namespace Sonata\AdminBundle\Twig\Extension;
 
 use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
 use Sonata\AdminBundle\Filter\FilterInterface;
+use Sonata\AdminBundle\Admin\NoValueException;
+
 use Symfony\Component\Form\FormView;
 
 class SonataAdminExtension extends \Twig_Extension
@@ -134,13 +136,13 @@ class SonataAdminExtension extends \Twig_Extension
             throw new \RuntimeException('remove the loop requirement');
         }
 
-        $value = $fieldDescription->getValue($object);
-
-        // no value defined, check if the fieldDescription point to an association
-        // if so, create an empty object instance
-        // fixme: not sure this is the best place to do that
-        if (!$value && $fieldDescription->getAssociationAdmin()) {
-            $value = $fieldDescription->getAssociationAdmin()->getNewInstance();
+        $value = null;
+        try {
+          $value = $fieldDescription->getValue($object);
+        } catch (NoValueException $e) {
+            if ($fieldDescription->getAssociationAdmin()) {
+                $value = $fieldDescription->getAssociationAdmin()->getNewInstance();
+            }
         }
 
         return $value;
@@ -176,10 +178,16 @@ class SonataAdminExtension extends \Twig_Extension
     {
         $template = $this->getTemplate($fieldDescription, 'SonataAdminBundle:CRUD:base_view_field.html.twig');
 
+        try {
+            $value = $fieldDescription->getValue($object);
+        } catch (NoValueException $e) {
+            $value = null;
+        }
+
         return $this->output($fieldDescription, $template, array(
             'field_description' => $fieldDescription,
             'object'            => $object,
-            'value'             => $fieldDescription->getValue($object)
+            'value'             => $value
         ));
     }