From 29fb9836b99ac4844884839c7061aa5cd1be5a3f Mon Sep 17 00:00:00 2001 From: Bart van den Burg Date: Tue, 3 Apr 2012 10:00:49 +0200 Subject: [PATCH 1/3] Added a clientToApp method for use in the BindClientData event --- src/Symfony/Component/Form/Form.php | 16 ++++++++++++++++ src/Symfony/Component/Form/Tests/FormTest.php | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index 0e68b88c01edf..f2c53a2f1c611 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -1089,6 +1089,22 @@ private function clientToNorm($value) return $value; } + /** + * Helper function for use in BIND_CLIENT_DATA event. Only allowed for child + * forms + * + * @params string $value The value to reverse transform + * + * @return mixed + */ + public function clientToApp($value) + { + if ($this->isRoot()) { + throw new \InvalidArgumentException('Cannot use clientToApp on a root form'); + } + return $this->normToApp($this->clientToNorm($value)); + } + /** * 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..555fcc0f09002 100644 --- a/src/Symfony/Component/Form/Tests/FormTest.php +++ b/src/Symfony/Component/Form/Tests/FormTest.php @@ -880,6 +880,25 @@ public function testBindMapsBoundChildrenOntoExistingClientData() )); } + public function testClientToApp() + { + $form = $this->getBuilder()->getForm(); + + $form->add($this->getBuilder('test') + ->appendClientTransformer(new FixedDataTransformer(array( + '' => '', + 'b' => 'a' + ))) + ->appendNormTransformer(new FixedDataTransformer(array( + '' => '', + 'c' => 'b' + ))) + ->getForm() + ); + + $this->assertEquals('c', $form->get('test')->clientToApp('a')); + } + public function testBindMapsBoundChildrenOntoEmptyData() { $test = $this; From c47f2dd408d87fc80423dbec710c4a6ac716714c Mon Sep 17 00:00:00 2001 From: Bart van den Burg Date: Tue, 3 Apr 2012 10:42:19 +0200 Subject: [PATCH 2/3] also included listeners in clientToApp method --- src/Symfony/Component/Form/Form.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index f2c53a2f1c611..65d1fa33aa7af 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -1102,7 +1102,14 @@ public function clientToApp($value) if ($this->isRoot()) { throw new \InvalidArgumentException('Cannot use clientToApp on a root form'); } - return $this->normToApp($this->clientToNorm($value)); + + $event = new DataEvent($this, $value); + $this->dispatcher->dispatch(FormEvents::PRE_BIND, $event); + + $event = new FilterDataEvent($this, $value); + $this->dispatcher->dispatch(FormEvents::BIND_CLIENT_DATA, $event); + + return $this->normToApp($this->clientToNorm($event->getData())); } /** From 8901efd78ed9e17c42b0f0009281d2019e9b3ec7 Mon Sep 17 00:00:00 2001 From: Bart van den Burg Date: Tue, 3 Apr 2012 11:08:08 +0200 Subject: [PATCH 3/3] updated method to accept a child name and client data to convert the value of its child --- src/Symfony/Component/Form/Form.php | 22 ++++++++++--------- src/Symfony/Component/Form/Tests/FormTest.php | 8 ++----- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index 65d1fa33aa7af..9be9af1b353b4 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -1090,26 +1090,28 @@ private function clientToNorm($value) } /** - * Helper function for use in BIND_CLIENT_DATA event. Only allowed for child - * forms + * Helper function for use in BIND_CLIENT_DATA event. * - * @params string $value The value to reverse transform + * @params string $name The name of the child field + * @params array $clientData the client data from the event * * @return mixed */ - public function clientToApp($value) + public function getChildNormData($name, $clientData) { - if ($this->isRoot()) { - throw new \InvalidArgumentException('Cannot use clientToApp on a root form'); + if (!$this->hasChildren()) { + throw new \InvalidArgumentException('This form has no children'); } - $event = new DataEvent($this, $value); + $form = $this->get($name); + + $event = new DataEvent($form, $clientData[$name]); $this->dispatcher->dispatch(FormEvents::PRE_BIND, $event); - $event = new FilterDataEvent($this, $value); + $event = new FilterDataEvent($form, $clientData[$name]); $this->dispatcher->dispatch(FormEvents::BIND_CLIENT_DATA, $event); - - return $this->normToApp($this->clientToNorm($event->getData())); + + return $form->clientToNorm($event->getData()); } /** diff --git a/src/Symfony/Component/Form/Tests/FormTest.php b/src/Symfony/Component/Form/Tests/FormTest.php index 555fcc0f09002..4ce66af15552f 100644 --- a/src/Symfony/Component/Form/Tests/FormTest.php +++ b/src/Symfony/Component/Form/Tests/FormTest.php @@ -889,14 +889,10 @@ public function testClientToApp() '' => '', 'b' => 'a' ))) - ->appendNormTransformer(new FixedDataTransformer(array( - '' => '', - 'c' => 'b' - ))) ->getForm() ); - - $this->assertEquals('c', $form->get('test')->clientToApp('a')); + + $this->assertEquals('b', $form->getChildNormData('test', array('test' => 'a'))); } public function testBindMapsBoundChildrenOntoEmptyData()