8000 [Form] Fixed expanded choice field to be marked invalid when unknown choices are submitted by webmozart · Pull Request #7940 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Fixed expanded choice field to be marked invalid when unknown choices are submitted #7940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 10, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ UPGRADE FROM 2.x to 3.0
```

* The `TypeTestCase` class was moved from the `Symfony\Component\Form\Tests\Extension\Core\Type` namespace to the `Symfony\Component\Form\Test` namespace.

Before:

```
Expand Down Expand Up @@ -162,6 +162,12 @@ UPGRADE FROM 2.x to 3.0
`NumberToLocalizedStringTransformer` were renamed to `ROUND_HALF_EVEN`,
`ROUND_HALF_UP` and `ROUND_HALF_DOWN`.

* The methods `ChoiceListInterface::getIndicesForChoices()` and
`ChoiceListInterface::getIndicesForValues()` were removed. No direct
replacement exists, although in most cases
`ChoiceListInterface::getChoicesForValues()` and
`ChoiceListInterface::getValuesForChoices()` should be sufficient.


### FrameworkBundle

Expand Down Expand Up @@ -249,7 +255,7 @@ UPGRADE FROM 2.x to 3.0
* The Locale component was removed and replaced by the Intl component.
Instead of the methods in `Symfony\Component\Locale\Locale`, you should use
these equivalent methods in `Symfony\Component\Intl\Intl` now:

* `Locale::getDisplayCountries()` -> `Intl::getRegionBundle()->getCountryNames()`
* `Locale::getCountries()` -> `array_keys(Intl::getRegionBundle()->getCountryNames())`
* `Locale::getDisplayLanguages()` -> `Intl::getLanguageBundle()->getLanguageNames()`
Expand Down
33 changes: 26 additions & 7 deletions src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,26 @@ public function getChoicesForValues(array $values)
// Optimize performance in case we have an entity loader and
// a single-field identifier
if ($this->idAsValue && $this->entityLoader) {
if (empty($values)) {
return array();
$unorderedEntities = $this->entityLoader->getEntitiesByIds($this->idField, $values);
$entitiesByValue = array();
$entities = array();

// Maintain order and indices from the given $values
// An alternative approach to the following loop is to add the
// "INDEX BY" clause to the Doctrine query in the loader,
// but I'm not sure whether that's doable in a generic fashion.
foreach ($unorderedEntities as $entity) {
$value = $this->fixValue(current($this->getIdentifierValues($entity)));
$entitiesByValue[$value] = $entity;
}

return $this->entityLoader->getEntitiesByIds($this->idField, $values);
foreach ($values as $i => $value) {
if (isset($entitiesByValue[$value])) {
$entities[$i] = $entitiesByValue[$value];
}
}

return $entities;
}

$this->load();
Expand Down Expand Up @@ -240,10 +255,10 @@ public function getValuesForChoices(array $entities)
if ($this->idAsValue) {
$values = array();

foreach ($entities as $entity) {
foreach ($entities as $i => $entity) {
if ($entity instanceof $this->class) {
// Make sure to convert to the right format
$values[] = $this->fixValue(current($this->getIdentifierValues($entity)));
$values[$i] = $this->fixValue(current($this->getIdentifierValues($entity)));
}
}

Expand All @@ -264,6 +279,8 @@ public function getValuesForChoices(array $entities)
* @return array
*
* @see Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
*
* @deprecated Deprecated since version 2.4, to be removed in 3.0.
*/
public function getIndicesForChoices(array $entities)
{
Expand All @@ -275,10 +292,10 @@ public function getIndicesForChoices(array $entities)
if ($this->idAsIndex) {
$indices = array();

foreach ($entities as $entity) {
foreach ($entities as $i => $entity) {
if ($entity instanceof $this->class) {
// Make sure to convert to the right format
$indices[] = $this->fixIndex(current($this->getIdentifierValues($entity)));
$indices[$i] = $this->fixIndex(current($this->getIdentifierValues($entity)));
}
}

Expand All @@ -299,6 +316,8 @@ public function getIndicesForChoices(array $entities)
* @return array
*
* @see Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
*
* @deprecated Deprecated since version 2.4, to be removed in 3.0.
*/
public function getIndicesForValues(array $values)
{
Expand Down
59 changes: 59 additions & 0 deletions src/Symfony/Bridge/Doctrine/Test/DoctrineTestHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Test;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\EntityManager;

/**
* Provides utility functions needed in tests.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class DoctrineTestHelper
{
/**
* Returns an entity manager for testing.
*
* @return EntityManager
*/
public static function createTestEntityManager()
{
if (!class_exists('PDO') || !in_array('sqlite', \PDO::getAvailableDrivers())) {
\PHPUnit_Framework_TestCase::markTestSkipped('This test requires SQLite support in your environment');
}

$config = new \Doctrine\ORM\Configuration();
$config->setEntityNamespaces(array('SymfonyTestsDoctrine' => 'Symfony\Bridge\Doctrine\Tests\Fixtures'));
$config->setAutoGenerateProxyClasses(true);
$config->setProxyDir(\sys_get_temp_dir());
$config->setProxyNamespace('SymfonyTests\Doctrine');
$config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader()));
$config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache());
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache());

$params = array(
'driver' => 'pdo_sqlite',
'memory' => true,
);

return EntityManager::create($params, $config);
}

/**
* This class cannot be instantiated.
*/
private function __construct()
{
}
}
33 changes: 10 additions & 23 deletions src/Symfony/Bridge/Doctrine/Tests/DoctrineOrmTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,21 @@

namespace Symfony\Bridge\Doctrine\Tests;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\EntityManager;
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper;

/**
* Class DoctrineOrmTestCase
*
* @deprecated Deprecated as of Symfony 2.4, to be removed in Symfony 3.0.
* Use {@link DoctrineTestHelper} instead.
*/
abstract class DoctrineOrmTestCase extends \PHPUnit_Framework_TestCase
{
/**
* @return EntityManager
* @return \Doctrine\ORM\EntityManager
*/
public static function createTestEntityManager($paths = array())
public static function createTestEntityManager()
{
if (!class_exists('PDO') || !in_array('sqlite', \PDO::getAvailableDrivers())) {
self::markTestSkipped('This test requires SQLite support in your environment');
}
$config = new \Doctrine\ORM\Configuration();
$config->setEntityNamespaces(array('SymfonyTestsDoctrine' => 'Symfony\Bridge\Doctrine\Tests\Fixtures'));
$config->setAutoGenerateProxyClasses(true);
$config->setProxyDir(\sys_get_temp_dir());
$config->setProxyNamespace('SymfonyTests\Doctrine');
$config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader()));
$config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache());
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache());

$params = array(
'driver' => 'pdo_sqlite',
'memory' => true,
);

return EntityManager::create($params, $config);
return DoctrineTestHelper::createTestEntityManager();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ class AssociationEntity
private $id;

/**
* @ORM\ManyToOne(targetEntity="SingleIdentEntity")
* @var \Symfony\Bridge\Doctrine\Tests\Form\Fixtures\SingleIdentEntity
* @ORM\ManyToOne(targetEntity="SingleIntIdEntity")
* @var \Symfony\Bridge\Doctrine\Tests\Fixtures\SingleIntIdEntity
*/
public $single;

/**
* @ORM\ManyToOne(targetEntity="CompositeIdentEntity")
* @ORM\ManyToOne(targetEntity="CompositeIntIdEntity")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="composite_id1", referencedColumnName="id1"),
* @ORM\JoinColumn(name="composite_id2", referencedColumnName="id2")
* })
* @var \Symfony\Bridge\Doctrine\Tests\Form\Fixtures\CompositeIdentEntity
* @var \Symfony\Bridge\Doctrine\Tests\Fixtures\CompositeIntIdEntity
*/
public $composite;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Tests\Fixtures;

use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;

/** @Entity */
class CompositeIntIdEntity
{
/** @Id @Column(type="integer") */
protected $id1;

/** @Id @Column(type="integer") */
protected $id2;

/** @Column(type="string") */
public $name;

public function __construct($id1, $id2, $name)
{
$this->id1 = $id1;
$this->id2 = $id2;
$this->name = $name;
}

public function __toString()
{
return $this->name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Doctrine\ORM\Mapping\Entity;

/** @Entity */
class CompositeStringIdentEntity
class CompositeStringIdEntity
{
/** @Id @Column(type="string") */
protected $id1;
Expand All @@ -33,4 +33,9 @@ public function __construct($id1, $id2, $name)
$this->id2 = $id2;
$this->name = $name;
}

public function __toString()
{
return $this->name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Doctrine\ORM\Mapping\Entity;

/** @Entity */
class DoubleIdentEntity
class DoubleNameEntity
{
/** @Id @Column(type="integer") */
protected $id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Doctrine\ORM\Mapping\Entity;

/** @Entity */
class ItemGroupEntity
class GroupableEntity
{
/** @Id @Column(type="integer") */
protected $id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Doctrine\ORM\Mapping\Entity;

/** @Entity */
class SingleIdentEntity
class SingleIntIdEntity
{
/** @Id @Column(type="integer") */
protected $id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Doctrine\ORM\Mapping\Entity;

/** @Entity */
class NoToStringSingleIdentEntity
class SingleIntIdNoToStringEntity
{
/** @Id @Column(type="integer") */
protected $id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Doctrine\ORM\Mapping\Entity;

/** @Entity */
class SingleStringIdentEntity
class SingleStringIdEntity
{
/** @Id @Column(type="string") */
protected $id;
Expand All @@ -29,4 +29,9 @@ public function __construct($id, $name)
$this->id = $id;
$this->name = $name;
}

public function __toString()
{
return $this->name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Symfony\Component\Security\Core\User\UserInterface;

/** @Entity */
class CompositeIdentEntity implements UserInterface
class User implements UserInterface
{
/** @Id @Column(type="integer") */
protected $id1;
Expand Down
Loading
0