From 6b3fbb59052cf5bf5fca789bc39fae5f74d99a3e Mon Sep 17 00:00:00 2001 From: Bernhard Schussek Date: Fri, 10 Jan 2014 13:53:52 +0100 Subject: [PATCH] [Form] Changed the default value of $flatten in Form::getErrors() to true --- UPGRADE-2.5.md | 2 +- UPGRADE-3.0.md | 6 +-- src/Symfony/Component/Form/Button.php | 2 +- src/Symfony/Component/Form/Form.php | 4 +- .../Component/Form/FormErrorIterator.php | 2 +- src/Symfony/Component/Form/FormInterface.php | 2 +- .../Component/Form/Tests/CompoundFormTest.php | 48 +++++++++---------- 7 files changed, 33 insertions(+), 33 deletions(-) diff --git a/UPGRADE-2.5.md b/UPGRADE-2.5.md index 57335a8c7b..f9fe7dda18 100644 --- a/UPGRADE-2.5.md +++ b/UPGRADE-2.5.md @@ -41,6 +41,6 @@ Form After: ``` - public function getErrors($deep = false, $flatten = false) + public function getErrors($deep = false, $flatten = true) { ``` diff --git a/UPGRADE-3.0.md b/UPGRADE-3.0.md index f566ce9852..4e2c3f9f92 100644 --- a/UPGRADE-3.0.md +++ b/UPGRADE-3.0.md @@ -181,8 +181,8 @@ UPGRADE FROM 2.x to 3.0 and "csrf_token_id". * The method `Form::getErrorsAsString()` was removed. Use `Form::getErrors()` - instead with the argument `$deep` set to true and cast the returned iterator - to a string (if not done implicitly by PHP). + instead with the argument `$deep` set to true and `$flatten` set to false + and cast the returned iterator to a string (if not done implicitly by PHP). Before: @@ -193,7 +193,7 @@ UPGRADE FROM 2.x to 3.0 After: ``` - echo $form->getErrors(true); + echo $form->getErrors(true, false); ``` diff --git a/src/Symfony/Component/Form/Button.php b/src/Symfony/Component/Form/Button.php index 14ffaef8bc..795404b417 100644 --- a/src/Symfony/Component/Form/Button.php +++ b/src/Symfony/Component/Form/Button.php @@ -184,7 +184,7 @@ class Button implements \IteratorAggregate, FormInterface /** * {@inheritdoc} */ - public function getErrors($deep = false, $flatten = false) + public function getErrors($deep = false, $flatten = true) { $errors = array(); diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index 418800d7c3..e05adda3e7 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -778,7 +778,7 @@ class Form implements \IteratorAggregate, FormInterface /** * {@inheritdoc} */ - public function getErrors($deep = false, $flatten = false) + public function getErrors($deep = false, $flatten = true) { return new FormErrorIterator($this->errors, $this, $deep, $flatten); } @@ -797,7 +797,7 @@ class Form implements \IteratorAggregate, FormInterface */ public function getErrorsAsString($level = 0) { - return self::indent((string) $this->getErrors(true), $level); + return self::indent((string) $this->getErrors(true, false), $level); } /** diff --git a/src/Symfony/Component/Form/FormErrorIterator.php b/src/Symfony/Component/Form/FormErrorIterator.php index c9dd4e5a67..32ddf2e485 100644 --- a/src/Symfony/Component/Form/FormErrorIterator.php +++ b/src/Symfony/Component/Form/FormErrorIterator.php @@ -68,7 +68,7 @@ class FormErrorIterator implements \RecursiveIterator, \SeekableIterator, \Array * @param Boolean $flatten Whether to flatten the recursive list of * errors into a flat list */ - public function __construct(array &$errors, FormInterface $form, $deep = false, $flatten = false) + public function __construct(array &$errors, FormInterface $form, $deep = false, $flatten = true) { $this->errors = &$errors; $this->form = $form; diff --git a/src/Symfony/Component/Form/FormInterface.php b/src/Symfony/Component/Form/FormInterface.php index 2665017179..3b7fb7b3a0 100644 --- a/src/Symfony/Component/Form/FormInterface.php +++ b/src/Symfony/Component/Form/FormInterface.php @@ -104,7 +104,7 @@ interface FormInterface extends \ArrayAccess, \Traversable, \Countable * @since 2.5 Since version 2.5 this method returns a * {@link FormErrorIterator} instance instead of an array */ - public function getErrors($deep = false, $flatten = false); + public function getErrors($deep = false, $flatten = true); /** * Updates the form with default data. diff --git a/src/Symfony/Component/Form/Tests/CompoundFormTest.php b/src/Symfony/Component/Form/Tests/CompoundFormTest.php index 59bfce7b9b..d72fd66390 100644 --- a/src/Symfony/Component/Form/Tests/CompoundFormTest.php +++ b/src/Symfony/Component/Form/Tests/CompoundFormTest.php @@ -872,6 +872,30 @@ class CompoundFormTest extends AbstractFormTest $errors = $this->form->getErrors(true); + $this->assertSame( + "ERROR: Error 1\n". + "ERROR: Error 2\n". + "ERROR: Nested Error\n", + (string) $errors + ); + + $this->assertSame( + array($error1, $error2, $nestedError), + iterator_to_array($errors) + ); + } + + public function testGetErrorsDeepRecursive() + { + $this->form->addError($error1 = new FormError('Error 1')); + $this->form->addError($error2 = new FormError('Error 2')); + + $childForm = $this->getBuilder('Child')->getForm(); + $childForm->addError($nestedError = new FormError('Nested Error')); + $this->form->add($childForm); + + $errors = $this->form->getErrors(true, false); + $this->assertSame( "ERROR: Error 1\n". "ERROR: Error 2\n". @@ -892,30 +916,6 @@ class CompoundFormTest extends AbstractFormTest $this->assertSame($nestedError, $nestedErrorsAsArray[0]); } - public function testGetErrorsDeepFlat() - { - $this->form->addError($error1 = new FormError('Error 1')); - $this->form->addError($error2 = new FormError('Error 2')); - - $childForm = $this->getBuilder('Child')->getForm(); - $childForm->addError($nestedError = new FormError('Nested Error')); - $this->form->add($childForm); - - $errors = $this->form->getErrors(true, true); - - $this->assertSame( - "ERROR: Error 1\n". - "ERROR: Error 2\n". - "ERROR: Nested Error\n", - (string) $errors - ); - - $this->assertSame( - array($error1, $error2, $nestedError), - iterator_to_array($errors) - ); - } - // Basic cases are covered in SimpleFormTest public function testCreateViewWithChildren() {