Skip to content

Commit

Permalink
[Form] Added hook method preprocessData() to FieldGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernhard Schussek authored and fabpot committed Nov 23, 2010
1 parent 4aa1224 commit f9e830c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
15 changes: 15 additions & 0 deletions src/Symfony/Component/Form/FieldGroup.php
Expand Up @@ -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);
Expand All @@ -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
*
Expand Down
22 changes: 17 additions & 5 deletions tests/Symfony/Tests/Component/Form/FieldGroupTest.php
Expand Up @@ -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'));
}

Expand Down

0 comments on commit f9e830c

Please sign in to comment.