From 73d56f7a1a7e2aaf7cb00230af25ca208330ce6d Mon Sep 17 00:00:00 2001 From: Koen Kuipers Date: Thu, 27 Mar 2014 13:42:59 +0100 Subject: [PATCH 1/2] Fixed bug in ChoiceType triggering a warning when not using utf-8 This fixes issue #10409 by not using json_encode anymore, but serialize instead. --- src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php index 692f91e95c..83fe717f0d 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php @@ -166,7 +166,7 @@ class ChoiceType extends AbstractType $choices = null !== $options['choices'] ? $options['choices'] : array(); // Reuse existing choice lists in order to increase performance - $hash = md5(json_encode(array($choices, $options['preferred_choices']))); + $hash = hash('sha256', serialize(array($choices, $options['preferred_choices']))); if (!isset($choiceListCache[$hash])) { $choiceListCache[$hash] = new SimpleChoiceList($choices, $options['preferred_choices']); From 18dc9a7f5f98b757afa7a223fe6c33729e154ba6 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Fri, 28 Mar 2014 11:13:11 +0100 Subject: [PATCH 2/2] Added test case for 4c6a2d15095c13b2a35751b2b2712b183be489c4 --- .../Extension/Core/Type/ChoiceTypeTest.php | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php index 3da49dfa6a..2e99c2c774 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/ChoiceTypeTest.php @@ -1216,6 +1216,47 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase )); } + // https://github.com/symfony/symfony/issues/10409 + public function testReuseNonUtf8ChoiceLists() + { + $form1 = $this->factory->createNamed('name', 'choice', null, array( + 'choices' => array( + 'meter' => 'm', + 'millimeter' => 'mm', + 'micrometer' => chr(181).'meter', + ), + )); + + $form2 = $this->factory->createNamed('name', 'choice', null, array( + 'choices' => array( + 'meter' => 'm', + 'millimeter' => 'mm', + 'micrometer' => chr(181).'meter', + ), + )); + + $form3 = $this->factory->createNamed('name', 'choice', null, array( + 'choices' => array( + 'meter' => 'm', + 'millimeter' => 'mm', + 'micrometer' => null, + ), + )); + + // $form1 and $form2 use the same ChoiceList + $this->assertSame( + $form1->getConfig()->getOption('choice_list'), + $form2->getConfig()->getOption('choice_list') + ); + + // $form3 doesn't, but used to use the same when using json_encode() + // instead of serialize for the hashing algorithm + $this->assertNotSame( + $form1->getConfig()->getOption('choice_list'), + $form3->getConfig()->getOption('choice_list') + ); + } + public function testInitializeWithDefaultObjectChoice() { $obj1 = (object) array('value' => 'a', 'label' => 'A');