From f9e830caa2b18d70edc4ac8e7e2d0dc57ca75326 Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Mon, 22 Nov 2010 23:15:29 +0100 Subject: [PATCH] [Form] Added hook method preprocessData() to FieldGroup --- src/Symfony/Component/Form/FieldGroup.php | 15 +++++++++++++ .../Tests/Component/Form/FieldGroupTest.php | 22 ++++++++++++++----- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Form/FieldGroup.php b/src/Symfony/Component/Form/FieldGroup.php index 4bb11a710e28..b2b86b71e62c 100644 --- a/src/Symfony/Component/Form/FieldGroup.php +++ b/src/Symfony/Component/Form/FieldGroup.php @@ -332,6 +332,8 @@ public function bind($taintedData) } } + $taintedData = $this->preprocessData($taintedData); + foreach ($taintedData as $key => $value) { if ($this->has($key)) { $this->fields[$key]->bind($value); @@ -358,6 +360,19 @@ public function bind($taintedData) } } + /** + * 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 * diff --git a/tests/Symfony/Tests/Component/Form/FieldGroupTest.php b/tests/Symfony/Tests/Component/Form/FieldGroupTest.php index 32f94aff0688..71357afe367e 100644 --- a/tests/Symfony/Tests/Component/Form/FieldGroupTest.php +++ b/tests/Symfony/Tests/Component/Form/FieldGroupTest.php @@ -130,16 +130,28 @@ public function testHasNoErrorsIfOnlyFieldHasErrors() $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')); }