Skip to content

Commit 8c5ddc5

Browse files
committed
Merge branch '2.8' into 3.0
* 2.8: [DependencyInjection] fix dumped YAML snytax Remove InputOption::VALUE_REQUIRED mode from $default parameter description as InputOption::setDefault() throws an exception only when called in InputOption::VALUE_NONE mode. In practice the $default value could still be accessed in InputOption::VALUE_REQUIRED mode in case InputOption was never set but accessed from InputDefinition::getOption() method [Form] Fixed violation mapping if multiple forms are using the same (or part of the same) property path fix FQCN in tests added by #17694 Fix locale and written standard inconsistencies for Norwegian translations [Form] [Validator] Fix locale inconsistencies in Norwegian translations [TwigBridge] Symfony 3.1 forward compatibility fixed CS [DependencyInjection] fixed exceptions thrown by get method of ContainerBuilder [Yaml] properly parse lists in object maps [FrameworkBundle] Remove unused private method. [Form] remove useless code in ResizeFormListener [Config] Fix EnumNodeDefinition to allow building enum nodes with one element fix choice_value option in EntityType and add some tests
2 parents f55fd78 + d593c30 commit 8c5ddc5

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

Form/ChoiceList/DoctrineChoiceLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function loadChoicesForValues(array $values, $value = null)
146146

147147
// Optimize performance in case we have an object loader and
148148
// a single-field identifier
149-
if (!$this->choiceList && $this->objectLoader && $this->idReader->isSingleId()) {
149+
if (null === $value && !$this->choiceList && $this->objectLoader && $this->idReader->isSingleId()) {
150150
$unorderedObjects = $this->objectLoader->getEntitiesByIds($this->idReader->getIdField(), $values);
151151
$objectsById = array();
152152
$objects = array();

Tests/Form/Type/EntityTypeTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,55 @@ public function testOverrideChoices()
753753
$this->assertSame('2', $field->getViewData());
754754
}
755755

756+
public function testOverrideChoicesValues()
757+
{
758+
$entity1 = new SingleIntIdEntity(1, 'Foo');
759+
$entity2 = new SingleIntIdEntity(2, 'Bar');
760+
761+
$this->persist(array($entity1, $entity2));
762+
763+
$field = $this->factory->createNamed('name', 'Symfony\Bridge\Doctrine\Form\Type\EntityType', null, array(
764+
'em' => 'default',
765+
'class' => self::SINGLE_IDENT_CLASS,
766+
'choice_label' => 'name',
767+
'choice_value' => 'name',
768+
));
769+
770+
$field->submit('Bar');
771+
772+
$this->assertEquals(array('Foo' => new ChoiceView($entity1, 'Foo', 'Foo'), 'Bar' => new ChoiceView($entity2, 'Bar', 'Bar')), $field->createView()->vars['choices']);
773+
$this->assertTrue($field->isSynchronized(), 'Field should be synchronized.');
774+
$this->assertSame($entity2, $field->getData(), 'Entity should be loaded by custom value.');
775+
$this->assertSame('Bar', $field->getViewData());
776+
}
777+
778+
public function testOverrideChoicesValuesWithCallable()
779+
{
780+
$entity1 = new GroupableEntity(1, 'Foo', 'BazGroup');
781+
$entity2 = new GroupableEntity(2, 'Bar', 'BooGroup');
782+
783+
$this->persist(array($entity1, $entity2));
784+
785+
$field = $this->factory->createNamed('name', 'Symfony\Bridge\Doctrine\Form\Type\EntityType', null, array(
786+
'em' => 'default',
787+
'class' => self::ITEM_GROUP_CLASS,
788+
'choice_label' => 'name',
789+
'choice_value' => function (GroupableEntity $entity) {
790+
return $entity->groupName.'/'.$entity->name;
791+
},
792+
));
793+
794+
$field->submit('BooGroup/Bar');
795+
796+
$this->assertEquals(array(
797+
'BazGroup/Foo' => new ChoiceView($entity1, 'BazGroup/Foo', 'Foo'),
798+
'BooGroup/Bar' => new ChoiceView($entity2, 'BooGroup/Bar', 'Bar'),
799+
), $field->createView()->vars['choices']);
800+
$this->assertTrue($field->isSynchronized(), 'Field should be synchronized.');
801+
$this->assertSame($entity2, $field->getData(), 'Entity should be loaded by custom value.');
802+
$this->assertSame('BooGroup/Bar', $field->getViewData());
803+
}
804+
756805
public function testGroupByChoices()
757806
{
758807
$item1 = new GroupableEntity(1, 'Foo', 'Group1');

0 commit comments

Comments
 (0)