Bladeren bron

[Form] Added hook method preprocessData() to FieldGroup

Bernhard Schussek 14 jaren geleden
bovenliggende
commit
f9e830caa2
2 gewijzigde bestanden met toevoegingen van 32 en 5 verwijderingen
  1. 15 0
      src/Symfony/Component/Form/FieldGroup.php
  2. 17 5
      tests/Symfony/Tests/Component/Form/FieldGroupTest.php

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

@@ -332,6 +332,8 @@ class FieldGroup extends Field implements \IteratorAggregate, FieldGroupInterfac
             }
         }
 
+        $taintedData = $this->preprocessData($taintedData);
+
         foreach ($taintedData as $key => $value) {
             if ($this->has($key)) {
                 $this->fields[$key]->bind($value);
@@ -358,6 +360,19 @@ class FieldGroup extends Field implements \IteratorAggregate, FieldGroupInterfac
         }
     }
 
+    /**
+     * Processes the bound data before it is passed to the individual fields
+     *
+     * The data is in the user format.
+     *
+     * @param  array $data
+     * @return array
+     */
+    protected function preprocessData(array $data)
+    {
+        return $data;
+    }
+
     /**
      * Returns whether this form was bound with extra fields
      *

+ 17 - 5
tests/Symfony/Tests/Component/Form/FieldGroupTest.php

@@ -130,16 +130,28 @@ class FieldGroupTest extends \PHPUnit_Framework_TestCase
         $this->assertFalse($group->hasErrors());
     }
 
-    public function testBindForwardsBoundValues()
+    public function testBindForwardsPreprocessedData()
     {
         $field = $this->createMockField('firstName');
-        $field->expects($this->once())
-                    ->method('bind')
-                    ->with($this->equalTo('Bernhard'));
 
-        $group = new TestFieldGroup('author');
+        $group = $this->getMock(
+            'Symfony\Tests\Component\Form\Fixtures\TestFieldGroup',
+            array('preprocessData'), // only mock preprocessData()
+            array('author')
+        );
+
+        // The data array is prepared directly after binding
+        $group->expects($this->once())
+              ->method('preprocessData')
+              ->with($this->equalTo(array('firstName' => 'Bernhard')))
+              ->will($this->returnValue(array('firstName' => 'preprocessed[Bernhard]')));
         $group->add($field);
 
+        // The preprocessed data is then forwarded to the fields
+        $field->expects($this->once())
+                    ->method('bind')
+                    ->with($this->equalTo('preprocessed[Bernhard]'));
+
         $group->bind(array('firstName' => 'Bernhard'));
     }