浏览代码

[Form] Locale can now only be set statically before creating a form/field, otherwise we have too many problems updating a field's state when the locale is changed

Bernhard Schussek 14 年之前
父节点
当前提交
78b69876d4

+ 5 - 29
src/Symfony/Component/Form/DateField.php

@@ -97,7 +97,11 @@ class DateField extends HybridField
         $this->addOption('widget', self::CHOICE, self::$widgets);
         $this->addOption('pattern');
 
-        $this->initFormatter();
+        $this->formatter = new \IntlDateFormatter(
+            $this->locale,
+            self::$intlFormats[$this->getOption('format')],
+            \IntlDateFormatter::NONE
+        );
 
         if ($this->getOption('type') === self::STRING) {
             $this->setNormalizationTransformer(new ReversedTransformer(
@@ -211,34 +215,6 @@ class DateField extends HybridField
         return '{{ year }}-{{ month }}-{{ day }}';
     }
 
-    /**
-     * Sets the locale of this field.
-     *
-     * @see Localizable
-     */
-    public function setLocale($locale)
-    {
-        parent::setLocale($locale);
-
-        $this->initFormatter();
-
-        if ($this->getOption('widget') === self::CHOICE) {
-            $this->addChoiceFields();
-        }
-    }
-
-    /**
-     * Initializes (or reinitializes) the formatter
-     */
-    protected function initFormatter()
-    {
-        $this->formatter = new \IntlDateFormatter(
-            $this->locale,
-            self::$intlFormats[$this->getOption('format')],
-            \IntlDateFormatter::NONE
-        );
-    }
-
     /**
      * Adds (or replaces if already added) the fields used when widget=CHOICE
      */

+ 1 - 18
src/Symfony/Component/Form/Field.php

@@ -73,10 +73,7 @@ class Field extends Configurable implements FieldInterface
         $this->addOption('normalization_transformer');
 
         $this->key = (string)$key;
-
-        if ($this->locale === null) {
-            $this->locale = class_exists('\Locale', false) ? \Locale::getDefault() : 'en';
-        }
+        $this->locale = FormConfiguration::getDefaultLocale();
 
         parent::__construct($options);
 
@@ -366,20 +363,6 @@ class Field extends Configurable implements FieldInterface
         return $this->errors;
     }
 
-    /**
-     * Sets the locale of this field.
-     *
-     * @see Localizable
-     */
-    public function setLocale($locale)
-    {
-        $this->locale = $locale;
-
-        if ($this->valueTransformer !== null && $this->valueTransformer instanceof Localizable) {
-            $this->valueTransformer->setLocale($locale);
-        }
-    }
-
     /**
      * Injects the locale into the given object, if set.
      *

+ 0 - 15
src/Symfony/Component/Form/FieldGroup.php

@@ -92,7 +92,6 @@ class FieldGroup extends Field implements \IteratorAggregate, FieldGroupInterfac
         $this->fields[$field->getKey()] = $field;
 
         $field->setParent($this);
-        $field->setLocale($this->locale);
 
         $data = $this->getTransformedData();
 
@@ -514,18 +513,4 @@ class FieldGroup extends Field implements \IteratorAggregate, FieldGroupInterfac
     {
         return count($this->fields);
     }
-
-    /**
-     * Sets the locale of this field.
-     *
-     * @see Localizable
-     */
-    public function setLocale($locale)
-    {
-        parent::setLocale($locale);
-
-        foreach ($this->fields as $field) {
-            $field->setLocale($locale);
-        }
-    }
 }

+ 1 - 1
src/Symfony/Component/Form/FieldInterface.php

@@ -18,7 +18,7 @@ use Symfony\Component\I18N\TranslatorInterface;
  *
  * @author     Bernhard Schussek <bernhard.schussek@symfony-project.com>
  */
-interface FieldInterface extends Localizable
+interface FieldInterface
 {
     /**
      * Marks a constraint violation in a form field

+ 0 - 4
src/Symfony/Component/Form/Form.php

@@ -57,10 +57,6 @@ class Form extends FieldGroup
             $this->enableCsrfProtection();
         }
 
-        if (FormConfiguration::getDefaultLocale() !== null) {
-            $this->setLocale(FormConfiguration::getDefaultLocale());
-        }
-
         parent::__construct($name, $options);
 
         // If data is passed to this constructor, objects from parent forms

+ 1 - 1
src/Symfony/Component/Form/FormConfiguration.php

@@ -41,7 +41,7 @@ class FormConfiguration
      */
     static public function getDefaultLocale()
     {
-        return self::$defaultLocale;
+        return isset(self::$defaultLocale) ? self::$defaultLocale : \Locale::getDefault();
     }
 
     /**

+ 6 - 16
tests/Symfony/Tests/Component/Form/DateFieldTest.php

@@ -5,14 +5,19 @@ namespace Symfony\Tests\Component\Form;
 require_once __DIR__ . '/DateTimeTestCase.php';
 
 use Symfony\Component\Form\DateField;
+use Symfony\Component\Form\FormConfiguration;
 
 class DateFieldTest extends DateTimeTestCase
 {
+    protected function setUp()
+    {
+        FormConfiguration::setDefaultLocale('de_AT');
+    }
+
     public function testBind_fromInput_dateTime()
     {
         $field = new DateField('name', array('widget' => 'input', 'type' => DateField::DATETIME));
 
-        $field->setLocale('de_AT');
         $field->bind('2.6.2010');
 
         $this->assertDateTimeEquals(new \DateTime('2010-06-02 UTC'), $field->getData());
@@ -23,7 +28,6 @@ class DateFieldTest extends DateTimeTestCase
     {
         $field = new DateField('name', array('widget' => 'input', 'type' => DateField::STRING));
 
-        $field->setLocale('de_AT');
         $field->bind('2.6.2010');
 
         $this->assertEquals('2010-06-02', $field->getData());
@@ -34,7 +38,6 @@ class DateFieldTest extends DateTimeTestCase
     {
         $field = new DateField('name', array('widget' => 'input', 'type' => DateField::TIMESTAMP));
 
-        $field->setLocale('de_AT');
         $field->bind('2.6.2010');
 
         $dateTime = new \DateTime('2010-06-02 UTC');
@@ -52,7 +55,6 @@ class DateFieldTest extends DateTimeTestCase
             'type' => DateField::RAW,
         ));
 
-        $field->setLocale('de_AT');
         $field->bind('2.6.2010');
 
         $output = array(
@@ -75,7 +77,6 @@ class DateFieldTest extends DateTimeTestCase
             'year' => '2010',
         );
 
-        $field->setLocale('de_AT');
         $field->bind($input);
 
         $dateTime = new \DateTime('2010-06-02 UTC');
@@ -94,7 +95,6 @@ class DateFieldTest extends DateTimeTestCase
             'year' => '',
         );
 
-        $field->setLocale('de_AT');
         $field->bind($input);
 
         $this->assertSame(null, $field->getData());
@@ -111,7 +111,6 @@ class DateFieldTest extends DateTimeTestCase
             'widget' => 'input',
         ));
 
-        $field->setLocale('de_AT');
         $field->setData('2010-06-02');
 
         $this->assertEquals('01.06.2010', $field->getDisplayedData());
@@ -124,7 +123,6 @@ class DateFieldTest extends DateTimeTestCase
             'years' => array(2010, 2011),
         ));
 
-        $field->setLocale('de_AT');
         $field->bind('2.6.2010');
 
         $this->assertTrue($field->isYearWithinRange());
@@ -137,7 +135,6 @@ class DateFieldTest extends DateTimeTestCase
             'years' => array(2010, 2011),
         ));
 
-        $field->setLocale('de_AT');
         $field->bind('');
 
         $this->assertTrue($field->isYearWithinRange());
@@ -150,7 +147,6 @@ class DateFieldTest extends DateTimeTestCase
             'years' => array(2010, 2012),
         ));
 
-        $field->setLocale('de_AT');
         $field->bind('2.6.2011');
 
         $this->assertFalse($field->isYearWithinRange());
@@ -163,7 +159,6 @@ class DateFieldTest extends DateTimeTestCase
             'months' => array(6, 7),
         ));
 
-        $field->setLocale('de_AT');
         $field->bind('2.6.2010');
 
         $this->assertTrue($field->isMonthWithinRange());
@@ -176,7 +171,6 @@ class DateFieldTest extends DateTimeTestCase
             'months' => array(6, 7),
         ));
 
-        $field->setLocale('de_AT');
         $field->bind('');
 
         $this->assertTrue($field->isMonthWithinRange());
@@ -189,7 +183,6 @@ class DateFieldTest extends DateTimeTestCase
             'months' => array(6, 8),
         ));
 
-        $field->setLocale('de_AT');
         $field->bind('2.7.2010');
 
         $this->assertFalse($field->isMonthWithinRange());
@@ -202,7 +195,6 @@ class DateFieldTest extends DateTimeTestCase
             'days' => array(6, 7),
         ));
 
-        $field->setLocale('de_AT');
         $field->bind('6.6.2010');
 
         $this->assertTrue($field->isDayWithinRange());
@@ -215,7 +207,6 @@ class DateFieldTest extends DateTimeTestCase
             'days' => array(6, 7),
         ));
 
-        $field->setLocale('de_AT');
         $field->bind('');
 
         $this->assertTrue($field->isDayWithinRange());
@@ -228,7 +219,6 @@ class DateFieldTest extends DateTimeTestCase
             'days' => array(6, 8),
         ));
 
-        $field->setLocale('de_AT');
         $field->bind('7.6.2010');
 
         $this->assertFalse($field->isDayWithinRange());

+ 0 - 48
tests/Symfony/Tests/Component/Form/FieldGroupTest.php

@@ -16,17 +16,6 @@ use Symfony\Tests\Component\Form\Fixtures\TestField;
 use Symfony\Tests\Component\Form\Fixtures\TestFieldGroup;
 
 
-abstract class FieldGroupTest_Field extends TestField
-{
-    public $locales = array();
-
-    public function setLocale($locale)
-    {
-        $this->locales[] = $locale;
-    }
-}
-
-
 class FieldGroupTest extends \PHPUnit_Framework_TestCase
 {
     public function testSupportsArrayAccess()
@@ -566,43 +555,6 @@ class FieldGroupTest extends \PHPUnit_Framework_TestCase
         $this->assertFalse($group->isMultipart());
     }
 
-    public function testLocaleIsPassedToField_SetBeforeAddingTheField()
-    {
-        $field = $this->getMock('Symfony\Component\Form\Field', array(), array(), '', false, false);
-        $field->expects($this->any())
-                    ->method('getKey')
-                    ->will($this->returnValue('firstName'));
-        $field->expects($this->once())
-                    ->method('setLocale')
-                    ->with($this->equalTo('de_DE'));
-
-        $group = new TestFieldGroup('author');
-        $group->setLocale('de_DE');
-        $group->add($field);
-    }
-
-    public function testLocaleIsPassedToField_SetAfterAddingTheField()
-    {
-        $field = $this->getMockForAbstractClass(__NAMESPACE__ . '\FieldGroupTest_Field', array(), '', false, false);
-        $field->expects($this->any())
-                    ->method('getKey')
-                    ->will($this->returnValue('firstName'));
-// DOESN'T WORK!
-//    $field = $this->getMock(__NAMESPACE__ . '\Fixtures\Field', array(), array(), '', false, false);
-//    $field->expects($this->once())
-//          ->method('setLocale')
-//          ->with($this->equalTo('de_AT'));
-//    $field->expects($this->once())
-//          ->method('setLocale')
-//          ->with($this->equalTo('de_DE'));
-
-        $group = new TestFieldGroup('author');
-        $group->add($field);
-        $group->setLocale('de_DE');
-
-        $this->assertEquals(array(class_exists('\Locale', false) ? \Locale::getDefault() : 'en', 'de_DE'), $field->locales);
-    }
-
     public function testSupportsClone()
     {
         $group = new TestFieldGroup('author');

+ 7 - 16
tests/Symfony/Tests/Component/Form/FieldTest.php

@@ -10,6 +10,7 @@ require_once __DIR__ . '/Fixtures/RequiredOptionsField.php';
 use Symfony\Component\Form\ValueTransformer\ValueTransformerInterface;
 use Symfony\Component\Form\PropertyPath;
 use Symfony\Component\Form\FieldError;
+use Symfony\Component\Form\FormConfiguration;
 use Symfony\Tests\Component\Form\Fixtures\Author;
 use Symfony\Tests\Component\Form\Fixtures\TestField;
 use Symfony\Tests\Component\Form\Fixtures\InvalidField;
@@ -134,28 +135,18 @@ class FieldTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('news_article_title', $this->field->getId());
     }
 
-//    public function testLocaleIsPassedToLocalizableValueTransformer_setLocaleCalledBefore()
-//    {
-//        $transformer = $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface');
-//        $transformer->expects($this->once())
-//                         ->method('setLocale')
-//                         ->with($this->equalTo('de_DE'));
-//
-//        $this->field->setLocale('de_DE');
-//        $this->field->setValueTransformer($transformer);
-//    }
-
-    public function testLocaleIsPassedToValueTransformer_setLocaleCalledAfter()
+    public function testLocaleIsPassedToValueTransformer()
     {
+        FormConfiguration::setDefaultLocale('de_DE');
+
         $transformer = $this->getMock('Symfony\Component\Form\ValueTransformer\ValueTransformerInterface');
-        $transformer->expects($this->exactly(2))
-                         ->method('setLocale'); // we can't test the params cause they differ :(
+        $transformer->expects($this->exactly(1))
+                         ->method('setLocale')
+                         ->with($this->equalTo('de_DE'));
 
         $field = new TestField('title', array(
             'value_transformer' => $transformer,
         ));
-
-        $field->setLocale('de_DE');
     }
 
     public function testIsRequiredReturnsOwnValueIfNoParent()

+ 0 - 16
tests/Symfony/Tests/Component/Form/FormTest.php

@@ -171,22 +171,6 @@ class FormTest extends \PHPUnit_Framework_TestCase
         $this->assertFalse($this->form->isCsrfTokenValid());
     }
 
-    public function testDefaultLocaleCanBeSet()
-    {
-        FormConfiguration::setDefaultLocale('de-DE-1996');
-        $form = new Form('author', new Author(), $this->validator);
-
-        $field = $this->getMock('Symfony\Component\Form\Field', array(), array(), '', false, false);
-        $field->expects($this->any())
-                    ->method('getKey')
-                    ->will($this->returnValue('firstName'));
-        $field->expects($this->once())
-                    ->method('setLocale')
-                    ->with($this->equalTo('de-DE-1996'));
-
-        $form->add($field);
-    }
-
     public function testValidationGroupsCanBeSet()
     {
         $form = new Form('author', new Author(), $this->validator);