[Form] Added hook method preprocessData() to FieldGroup
This commit is contained in:
parent
4aa12248c5
commit
f9e830caa2
@ -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
|
||||
*
|
||||
|
@ -130,15 +130,27 @@ class FieldGroupTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertFalse($group->hasErrors());
|
||||
}
|
||||
|
||||
public function testBindForwardsBoundValues()
|
||||
public function testBindForwardsPreprocessedData()
|
||||
{
|
||||
$field = $this->createMockField('firstName');
|
||||
|
||||
$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('Bernhard'));
|
||||
|
||||
$group = new TestFieldGroup('author');
|
||||
$group->add($field);
|
||||
->with($this->equalTo('preprocessed[Bernhard]'));
|
||||
|
||||
$group->bind(array('firstName' => 'Bernhard'));
|
||||
}
|
||||
|
Reference in New Issue
Block a user