diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index 0e68b88c01edf..9be9af1b353b4 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -1089,6 +1089,31 @@ private function clientToNorm($value) return $value; } + /** + * Helper function for use in BIND_CLIENT_DATA event. + * + * @params string $name The name of the child field + * @params array $clientData the client data from the event + * + * @return mixed + */ + public function getChildNormData($name, $clientData) + { + if (!$this->hasChildren()) { + throw new \InvalidArgumentException('This form has no children'); + } + + $form = $this->get($name); + + $event = new DataEvent($form, $clientData[$name]); + $this->dispatcher->dispatch(FormEvents::PRE_BIND, $event); + + $event = new FilterDataEvent($form, $clientData[$name]); + $this->dispatcher->dispatch(FormEvents::BIND_CLIENT_DATA, $event); + + return $form->clientToNorm($event->getData()); + } + /** * Validates whether the given variable is a valid form name. * diff --git a/src/Symfony/Component/Form/Tests/FormTest.php b/src/Symfony/Component/Form/Tests/FormTest.php index d6784f4e52393..4ce66af15552f 100644 --- a/src/Symfony/Component/Form/Tests/FormTest.php +++ b/src/Symfony/Component/Form/Tests/FormTest.php @@ -880,6 +880,21 @@ public function testBindMapsBoundChildrenOntoExistingClientData() )); } + public function testClientToApp() + { + $form = $this->getBuilder()->getForm(); + + $form->add($this->getBuilder('test') + ->appendClientTransformer(new FixedDataTransformer(array( + '' => '', + 'b' => 'a' + ))) + ->getForm() + ); + + $this->assertEquals('b', $form->getChildNormData('test', array('test' => 'a'))); + } + public function testBindMapsBoundChildrenOntoEmptyData() { $test = $this;