From 9300e61f467034fde4151ef6ed9eae2086d7bbe6 Mon Sep 17 00:00:00 2001 From: Thomas Lallement Date: Thu, 28 Mar 2013 20:37:03 +0100 Subject: [PATCH 1/2] [Form] Add getErrorsAsArray method Usefull to return form errors through JsonResponse modified: src/Symfony/Component/Form/Form.php modified: src/Symfony/Component/Form/Tests/CompoundFormTest.php modified: src/Symfony/Component/Form/Tests/SimpleFormTest.php --- src/Symfony/Component/Form/Form.php | 27 ++++++++ .../Component/Form/Tests/CompoundFormTest.php | 66 +++++++++++++++++++ .../Component/Form/Tests/SimpleFormTest.php | 7 ++ 3 files changed, 100 insertions(+) diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index 401d7a2872b9..ef9a7b608f12 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -633,6 +633,33 @@ public function getErrorsAsString($level = 0) return $errors; } + /** + * Returns an array representation of all form errors (including children errors). + * + * @return array An array representation of all errors + */ + public function getErrorsAsArray() + { + $errors = array(); + foreach ($this->errors as $key => $error) { + $template = $error->getMessageTemplate(); + if (null !== $template) { + $parameters = $error->getMessageParameters(); + $errors[$key] = strtr($template, $parameters); + } else { + $errors[$key] = $error->getMessage(); + } + } + + foreach ($this->children as $child) { + if (!$child->isValid()) { + $errors[$child->getName()] = $child->getErrorsAsArray(); + } + } + + return $errors; + } + /** * {@inheritdoc} */ diff --git a/src/Symfony/Component/Form/Tests/CompoundFormTest.php b/src/Symfony/Component/Form/Tests/CompoundFormTest.php index 06e9b4d19e57..19cd657cff42 100644 --- a/src/Symfony/Component/Form/Tests/CompoundFormTest.php +++ b/src/Symfony/Component/Form/Tests/CompoundFormTest.php @@ -635,6 +635,72 @@ public function testGetErrorsAsStringDeep() $this->assertEquals("name:\n ERROR: Error!\nfoo:\n No errors\n", $parent->getErrorsAsString()); } + public function testGetErrorsAsArrayDeep() + { + if (!class_exists('Symfony\Component\HttpFoundation\Request')) { + $this->markTestSkipped('The "HttpFoundation" component is not available'); + } + + $request = new Request(array(), array(), array(), array(), array(), array( + 'REQUEST_METHOD' => 'GET', + )); + + $form = $this->getBuilder('') + ->setCompound(true) + ->setDataMapper($this->getDataMapper()) + ->addEventSubscriber(new BindRequestListener()) + ->getForm(); + + $firstName = $this->getBuilder('firstName')->getForm(); + + $form->add($firstName); + $form->add($this->getBuilder('lastName')->getForm()); + + $form->bind($request); + + $firstName->addError(new FormError('Error 1')); + $firstName->addError(new FormError('Error 2')); + + $expected = array( + 'firstName' => array('Error 1', 'Error 2'), + ); + + $this->assertEquals($expected, $form->getErrorsAsArray()); + } + + public function testGetErrorsAsArrayWithTemplate() + { + if (!class_exists('Symfony\Component\HttpFoundation\Request')) { + $this->markTestSkipped('The "HttpFoundation" component is not available'); + } + + $request = new Request(array(), array(), array(), array(), array(), array( + 'REQUEST_METHOD' => 'GET', + )); + + $form = $this->getBuilder('') + ->setCompound(true) + ->setDataMapper($this->getDataMapper()) + ->addEventSubscriber(new BindRequestListener()) + ->getForm(); + + $firstName = $this->getBuilder('firstName')->getForm(); + + $form->add($firstName); + $form->add($this->getBuilder('lastName')->getForm()); + + $form->bind($request); + + $firstName->addError(new FormError('Error 1', 'Error {{ replacementTag }}', array('{{ replacementTag }}' => 'foo'))); + $firstName->addError(new FormError('Error 2')); + + $expected = array( + 'firstName' => array('Error foo', 'Error 2'), + ); + + $this->assertEquals($expected, $form->getErrorsAsArray()); + } + protected function createForm() { return $this->getBuilder() diff --git a/src/Symfony/Component/Form/Tests/SimpleFormTest.php b/src/Symfony/Component/Form/Tests/SimpleFormTest.php index c26991959984..2ebddd456fa5 100644 --- a/src/Symfony/Component/Form/Tests/SimpleFormTest.php +++ b/src/Symfony/Component/Form/Tests/SimpleFormTest.php @@ -705,6 +705,13 @@ public function testGetErrorsAsString() $this->assertEquals("ERROR: Error!\n", $this->form->getErrorsAsString()); } + public function testGetErrorsAsArray() + { + $this->form->addError(new FormError('Error!')); + + $this->assertEquals(array("Error!"), $this->form->getErrorsAsArray()); + } + public function testFormCanHaveEmptyName() { $form = $this->getBuilder('')->getForm(); From 5f9d70c94d9bd8ecffdae1b2a8e40077e79c8662 Mon Sep 17 00:00:00 2001 From: Thomas Lallement Date: Thu, 28 Mar 2013 20:53:00 +0100 Subject: [PATCH 2/2] [Form][Test] Fix indentation modified: src/Symfony/Component/Form/Tests/CompoundFormTest.php --- src/Symfony/Component/Form/Tests/CompoundFormTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Form/Tests/CompoundFormTest.php b/src/Symfony/Component/Form/Tests/CompoundFormTest.php index 19cd657cff42..508561b1a10d 100644 --- a/src/Symfony/Component/Form/Tests/CompoundFormTest.php +++ b/src/Symfony/Component/Form/Tests/CompoundFormTest.php @@ -679,10 +679,10 @@ public function testGetErrorsAsArrayWithTemplate() )); $form = $this->getBuilder('') - ->setCompound(true) - ->setDataMapper($this->getDataMapper()) - ->addEventSubscriber(new BindRequestListener()) - ->getForm(); + ->setCompound(true) + ->setDataMapper($this->getDataMapper()) + ->addEventSubscriber(new BindRequestListener()) + ->getForm(); $firstName = $this->getBuilder('firstName')->getForm();