소스 검색

[Form] Fixed: Objects are stored in the form before calling configure()

Bernhard Schussek 15 년 전
부모
커밋
34dd0ea25b
3개의 변경된 파일30개의 추가작업 그리고 3개의 파일을 삭제
  1. 4 1
      src/Symfony/Components/Form/Field.php
  2. 2 2
      src/Symfony/Components/Form/Form.php
  3. 24 0
      tests/Symfony/Tests/Components/Form/FormTest.php

+ 4 - 1
src/Symfony/Components/Form/Field.php

@@ -42,9 +42,12 @@ abstract class Field extends Configurable implements FieldInterface
         $this->addOption('property_path', (string)$key);
 
         $this->key = (string)$key;
-        $this->locale = class_exists('\Locale', false) ? \Locale::getDefault() : 'en';
         $this->generator = new HtmlGenerator();
 
+        if ($this->locale === null) {
+            $this->locale = class_exists('\Locale', false) ? \Locale::getDefault() : 'en';
+        }
+
         parent::__construct($options);
 
         $this->transformedData = $this->transform($this->data);

+ 2 - 2
src/Symfony/Components/Form/Form.php

@@ -58,8 +58,6 @@ class Form extends FieldGroup
         $this->generator = new HtmlGenerator();
         $this->validator = $validator;
 
-        parent::__construct($name, $options);
-
         $this->setData($object);
         $this->setCsrfFieldName(self::$defaultCsrfFieldName);
 
@@ -80,6 +78,8 @@ class Form extends FieldGroup
         if (self::$defaultTranslator !== null) {
             $this->setTranslator(self::$defaultTranslator);
         }
+
+        parent::__construct($name, $options);
     }
 
     /**

+ 24 - 0
tests/Symfony/Tests/Components/Form/FormTest.php

@@ -25,6 +25,25 @@ class FormTest_PreconfiguredForm extends Form
     }
 }
 
+class TestSetDataBeforeConfigureForm extends Form
+{
+    protected $testCase;
+    protected $object;
+
+    public function __construct($testCase, $name, $object, $validator)
+    {
+        $this->testCase = $testCase;
+        $this->object = $object;
+
+        parent::__construct($name, $object, $validator);
+    }
+
+    protected function configure()
+    {
+        $this->testCase->assertEquals($this->object, $this->getData());
+    }
+}
+
 class FormTest extends \PHPUnit_Framework_TestCase
 {
     protected $validator;
@@ -43,6 +62,11 @@ class FormTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals(new Author(), $this->form->getData());
     }
 
+    public function testSetDataBeforeConfigure()
+    {
+        new TestSetDataBeforeConfigureForm($this, 'author', new Author(), $this->validator);
+    }
+
     public function testIsCsrfProtected()
     {
         $this->assertFalse($this->form->isCsrfProtected());