Skip to content

Commit

Permalink
Fix sonata-project#171 - UploadedFile could not be converted to boole…
Browse files Browse the repository at this point in the history
…an error
  • Loading branch information
Thomas Rabaix committed Jun 27, 2011
1 parent 86676d8 commit 03ec3fb
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 14 deletions.
6 changes: 5 additions & 1 deletion Admin/AdminHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ public function appendFormFieldElement(AdminInterface $admin, $elementId)
// 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());
Expand Down
2 changes: 1 addition & 1 deletion Admin/BaseFieldDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public function getValue($object)
}
}

return false;
throw new NoValueException();
}

/**
Expand Down
17 changes: 17 additions & 0 deletions Admin/NoValueException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the Sonata package.
*
* (c) 2010-2011 Thomas Rabaix <[email protected]>
*
* 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
{

}
8 changes: 7 additions & 1 deletion Builder/ORM/FormContractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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;
Expand Down Expand Up @@ -229,7 +230,12 @@ public function addField(FormBuilder $formBuilder, FieldDescriptionInterface $fi
// 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']));
Expand Down
6 changes: 3 additions & 3 deletions Model/ORM/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ public function delete($object)

/**
* 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)
{
Expand Down Expand Up @@ -190,7 +190,7 @@ public function getIdentifierFieldNames($class)
public function getNormalizedIdentifier($entity)
{
// the entities is not managed
if (!$this->getEntityManager()->getUnitOfWork()->isInIdentityMap($entity)) {
if (!$entity || !$this->getEntityManager()->getUnitOfWork()->isInIdentityMap($entity)) {
return null;
}

Expand Down
24 changes: 16 additions & 8 deletions Twig/Extension/SonataAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

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
Expand Down Expand Up @@ -134,13 +136,13 @@ public function getValueFromFieldDescription($object, FieldDescriptionInterface
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;
Expand Down Expand Up @@ -176,10 +178,16 @@ public function renderViewElement(FieldDescriptionInterface $fieldDescription, $
{
$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
));
}

Expand Down

0 comments on commit 03ec3fb

Please sign in to comment.