From 725d9eefd0e60c53426234641ebb60f1b2ff6551 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Sun, 22 Sep 2013 20:14:19 +0200 Subject: [PATCH 1/3] Created FormErrorBag This is fully BC with returning an array and it deprecates Form::getErrorsAsString() --- src/Symfony/Component/Form/CHANGELOG.md | 6 + src/Symfony/Component/Form/Form.php | 55 ++--- src/Symfony/Component/Form/FormErrorBag.php | 233 ++++++++++++++++++ src/Symfony/Component/Form/FormInterface.php | 2 +- .../ViolationMapper/ViolationMapperTest.php | 74 ++++-- .../Component/Form/Tests/FormErrorBagTest.php | 119 +++++++++ .../Component/Form/Tests/SimpleFormTest.php | 2 +- 7 files changed, 446 insertions(+), 45 deletions(-) create mode 100644 src/Symfony/Component/Form/FormErrorBag.php create mode 100644 src/Symfony/Component/Form/Tests/FormErrorBagTest.php diff --git a/src/Symfony/Component/Form/CHANGELOG.md b/src/Symfony/Component/Form/CHANGELOG.md index b9c8256b5a055..e08fb8fc184e2 100644 --- a/src/Symfony/Component/Form/CHANGELOG.md +++ b/src/Symfony/Component/Form/CHANGELOG.md @@ -4,6 +4,12 @@ CHANGELOG 2.5.0 ------ + * [BC BREAK] changed getErrors() to return a FormErrorBag, which is countable, + traversable and can be converted to a string. This breaks BC if the return + value is used in array function like is_array(). + * deprecated getErrorsAsString() in favor of converting getErrors() to a + string + * added an option for multiple files upload 2.4.0 diff --git a/src/Symfony/Component/Form/Form.php b/src/Symfony/Component/Form/Form.php index e677fff62993d..09510de0728d7 100644 --- a/src/Symfony/Component/Form/Form.php +++ b/src/Symfony/Component/Form/Form.php @@ -79,10 +79,11 @@ class Form implements \IteratorAggregate, FormInterface private $children; /** - * The errors of this form - * @var FormError[] An array of FormError instances + * The errors of this form. + * + * @var FormErrorBag */ - private $errors = array(); + private $errors; /** * Whether this form was submitted @@ -172,6 +173,7 @@ public function __construct(FormConfigInterface $config) $this->config = $config; $this->children = new OrderedHashMap(); + $this->errors = new FormErrorBag(); } public function __clone() @@ -501,7 +503,7 @@ public function submit($submittedData, $clearMissing = true) // Initialize errors in the very beginning so that we don't lose any // errors added during listeners - $this->errors = array(); + $this->errors = new FormErrorBag(); // Obviously, a disabled form should not change its data upon submission. if ($this->isDisabled()) { @@ -675,7 +677,7 @@ public function addError(FormError $error) if ($this->parent && $this->config->getErrorBubbling()) { $this->parent->addError($error); } else { - $this->errors[] = $error; + $this->errors->addError($error); } return $this; @@ -776,37 +778,34 @@ public function getClickedButton() */ public function getErrors() { - return $this->errors; - } - - /** - * Returns a string representation of all form errors (including children errors). - * - * This method should only be used to help debug a form. - * - * @param integer $level The indentation level (used internally) - * - * @return string A string representation of all errors - */ - public function getErrorsAsString($level = 0) - { - $errors = ''; - foreach ($this->errors as $error) { - $errors .= str_repeat(' ', $level).'ERROR: '.$error->getMessage()."\n"; - } + $errors = clone $this->errors; foreach ($this->children as $key => $child) { - $errors .= str_repeat(' ', $level).$key.":\n"; - if ($child instanceof self && $err = $child->getErrorsAsString($level + 4)) { - $errors .= $err; - } else { - $errors .= str_repeat(' ', $level + 4)."No errors\n"; + $childrenErrors = $child->getErrors(); + + // for BC + if (is_array($childrenErrors)) { + $_collection = new FormErrorBag(); + foreach ($childrenErrors as $error) { + $_collection->addError($error); + } + $childrenErrors = $_collection; } + + $errors->addCollection($key, $childrenErrors); } return $errors; } + /** + * @deprecated Deprecated since version 2.5, to be removed in 3.0. Convert {@link getErrors()} to a string instead. + */ + public function getErrorsAsString($level = 0) + { + return $this->getErrors()->__toString($level); + } + /** * {@inheritdoc} */ diff --git a/src/Symfony/Component/Form/FormErrorBag.php b/src/Symfony/Component/Form/FormErrorBag.php new file mode 100644 index 0000000000000..235ef221c41d1 --- /dev/null +++ b/src/Symfony/Component/Form/FormErrorBag.php @@ -0,0 +1,233 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form; + +use Symfony\Component\Form\Exception\InvalidArgumentException; + +/** + * A bag of Form Errors. + * + * @author Wouter J + * + * @since v2.5 + */ +class FormErrorBag implements \RecursiveIterator, \Countable, \ArrayAccess +{ + /** + * @var array An array of FormError and FormErrorBag instances + */ + protected $errors = array(); + + private $formName; + + public function setFormName($name) + { + $this->formName = $name; + } + + /** + * Adds a new form error. + * + * @param FormError $error + */ + public function addError(FormError $error) + { + $this->errors[] = $error; + } + + /** + * Adds a new form error collection. + * + * @param string $formName + * @param FormErrorBag $collection + */ + public function addCollection($formName, $collection) + { + $collection->setFormName($formName); + + $this->errors[$formName] = $collection; + } + + public function current() + { + $current = current($this->errors); + + if (!$current instanceof FormError) { + $this->next(); + + if ($this->valid()) { + $current = $this->current(); + } + } + + return $current; + } + + public function key() + { + return isset($this->formName) ? $this->formName : key($this->errors); + } + + public function next() + { + return next($this->errors); + } + + public function rewind() + { + reset($this->errors); + } + + public function valid() + { + return false === key($this->errors); + } + + /** + * {@inheritDoc} + */ + public function hasChildren() + { + return current($this->errors) instanceof FormErrorBag; + } + + /** + * {@inheritDoc} + */ + public function getChildren() + { + return current($this->errors); + } + + public function count() + { + $count = 0; + + foreach ($this->errors as $error) { + if ($error instanceof FormError) { + $count++; + } + } + + return $count; + } + + /** + * Counts all errors, including errors from children. + * + * @return int + */ + public function countAll() + { + $count = 0; + + foreach ($this->errors as $error) { + if ($error instanceof FormErrorBag) { + $count += $error->countAll(); + } else { + $count++; + } + } + + return $count; + } + + public function get($offset) + { + return $this->errors[$offset]; + } + + public function set($offset, $value) + { + $this->errors[$offset] = $value; + } + + public function has($offset) + { + return isset($this->errors[$offset]); + } + + public function all() + { + return $this->errors; + } + + public function clear() + { + $this->replace(); + } + + public function remove($offset) + { + unset($this->errors[$offset]); + } + + public function replace(array $errors = array()) + { + $this->errors = $errors; + } + + public function isEmpty() + { + return empty($this->errors); + } + + public function keys() + { + return array_keys($this->errors); + } + + public function __toString() + { + $level = func_num_args() > 0 ? func_get_arg(0) : 0; + $errors = ''; + + foreach ($this->errors as $key => $error) { + if ($error instanceof self) { + $errors .= str_repeat(' ', $level).$key.":\n"; + if ($err = $error->__toString($level + 4)) { + $errors .= $err; + } else { + $errors .= str_repeat(' ', $level + 4)."No errors\n"; + } + } else { + $errors .= str_repeat(' ', $level).'ERROR: '.$error->getMessage()."\n"; + } + } + + return $errors; + } + + public function offsetExists($offset) + { + return $this->has($offset) && $this->errors[$offset] instanceof FormError; + } + + public function offsetGet($offset) + { + $error = $this->get($offset); + + if ($error instanceof FormError) { + return $error; + } + } + + public function offsetSet($offset, $value) + { + return $this->set($offset); + } + + public function offsetUnset($offset) + { + return $this->remove($offset); + } +} diff --git a/src/Symfony/Component/Form/FormInterface.php b/src/Symfony/Component/Form/FormInterface.php index 5a852e864fdd6..b5126cd19479d 100644 --- a/src/Symfony/Component/Form/FormInterface.php +++ b/src/Symfony/Component/Form/FormInterface.php @@ -94,7 +94,7 @@ public function all(); /** * Returns all errors. * - * @return FormError[] An array of FormError instances that occurred during validation + * @return FormErrorBag */ public function getErrors(); diff --git a/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php b/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php index a84d5b951ae14..4bc923ea2ae5a 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php @@ -17,6 +17,7 @@ use Symfony\Component\Form\Form; use Symfony\Component\Form\FormConfigBuilder; use Symfony\Component\Form\FormError; +use Symfony\Component\Form\FormErrorBag; use Symfony\Component\PropertyAccess\PropertyPath; use Symfony\Component\Validator\ConstraintViolation; @@ -127,7 +128,11 @@ public function testMapToFormInheritingParentDataIfDataDoesNotMatch() $this->mapper->mapViolation($violation, $parent); $this->assertCount(0, $parent->getErrors(), $parent->getName().' should not have an error, but has one'); - $this->assertEquals(array($this->getFormError()), $child->getErrors(), $child->getName().' should have an error, but has none'); + + $childErrors = new FormErrorBag(); + $childErrors->addError($this->getFormError()); + $childErrors->addCollection($grandChild->getName(), new FormErrorBag()); + $this->assertEquals($childErrors, $child->getErrors(), $child->getName().' should have an error, but has none'); $this->assertCount(0, $grandChild->getErrors(), $grandChild->getName().' should not have an error, but has one'); } @@ -154,7 +159,10 @@ public function testFollowDotRules() $this->assertCount(0, $parent->getErrors(), $parent->getName().' should not have an error, but has one'); $this->assertCount(0, $child->getErrors(), $child->getName().' should not have an error, but has one'); $this->assertCount(0, $grandChild->getErrors(), $grandChild->getName().' should not have an error, but has one'); - $this->assertEquals(array($this->getFormError()), $grandGrandChild->getErrors(), $grandGrandChild->getName().' should have an error, but has none'); + + $grandGrandChildErrors = new FormErrorBag(); + $grandGrandChildErrors->addError($this->getFormError()); + $this->assertEquals($grandGrandChildErrors, $grandGrandChild->getErrors(), $grandGrandChild->getName().' should have an error, but has none'); } public function testAbortMappingIfNotSynchronized() @@ -744,18 +752,27 @@ public function testDefaultErrorMapping($target, $childName, $childPath, $grandC $this->mapper->mapViolation($violation, $parent); + $expectedErrors = new FormErrorBag(); + $expectedErrors->addError($this->getFormError()); + if (self::LEVEL_0 === $target) { - $this->assertEquals(array($this->getFormError()), $parent->getErrors(), $parent->getName().' should have an error, but has none'); + $childErrors = new FormErrorBag(); + $childErrors->addCollection($grandChildName, new FormErrorBag()); + $expectedErrors->addCollection($childName, $childErrors); + + $this->assertEquals($expectedErrors, $parent->getErrors(), $parent->getName().' should have an error, but has none'); $this->assertCount(0, $child->getErrors(), $childName.' should not have an error, but has one'); $this->assertCount(0, $grandChild->getErrors(), $grandChildName.' should not have an error, but has one'); } elseif (self::LEVEL_1 === $target) { + $expectedErrors->addCollection($grandChildName, new FormErrorBag()); + $this->assertCount(0, $parent->getErrors(), $parent->getName().' should not have an error, but has one'); - $this->assertEquals(array($this->getFormError()), $child->getErrors(), $childName.' should have an error, but has none'); + $this->assertEquals($expectedErrors, $child->getErrors(), $childName.' should have an error, but has none'); $this->assertCount(0, $grandChild->getErrors(), $grandChildName.' should not have an error, but has one'); } else { $this->assertCount(0, $parent->getErrors(), $parent->getName().' should not have an error, but has one'); $this->assertCount(0, $child->getErrors(), $childName.' should not have an error, but has one'); - $this->assertEquals(array($this->getFormError()), $grandChild->getErrors(), $grandChildName.' should have an error, but has none'); + $this->assertEquals($expectedErrors, $grandChild->getErrors(), $grandChildName.' should have an error, but has none'); } } @@ -1216,18 +1233,27 @@ public function testCustomDataErrorMapping($target, $mapFrom, $mapTo, $childName $this->assertCount(0, $distraction->getErrors(), 'distraction should not have an error, but has one'); } + $expectedErrors = new FormErrorBag(); + $expectedErrors->addError($this->getFormError()); + if (self::LEVEL_0 === $target) { - $this->assertEquals(array($this->getFormError()), $parent->getErrors(), $parent->getName().' should have an error, but has none'); + $childErrors = new FormErrorBag(); + $childErrors->addCollection($grandChildName, new FormErrorBag()); + $expectedErrors->addCollection($childName, $childErrors); + + $this->assertEquals($expectedErrors, $parent->getErrors(), $parent->getName().' should have an error, but has none'); $this->assertCount(0, $child->getErrors(), $childName.' should not have an error, but has one'); $this->assertCount(0, $grandChild->getErrors(), $grandChildName.' should not have an error, but has one'); } elseif (self::LEVEL_1 === $target) { + $expectedErrors->addCollection($grandChildName, new FormErrorBag()); + $this->assertCount(0, $parent->getErrors(), $parent->getName().' should not have an error, but has one'); - $this->assertEquals(array($this->getFormError()), $child->getErrors(), $childName.' should have an error, but has none'); + $this->assertEquals($expectedErrors, $child->getErrors(), $childName.' should have an error, but has none'); $this->assertCount(0, $grandChild->getErrors(), $grandChildName.' should not have an error, but has one'); } else { $this->assertCount(0, $parent->getErrors(), $parent->getName().' should not have an error, but has one'); $this->assertCount(0, $child->getErrors(), $childName.' should not have an error, but has one'); - $this->assertEquals(array($this->getFormError()), $grandChild->getErrors(), $grandChildName.' should have an error, but has none'); + $this->assertEquals($expectedErrors, $grandChild->getErrors(), $grandChildName.' should have an error, but has none'); } } @@ -1397,18 +1423,27 @@ public function testCustomFormErrorMapping($target, $mapFrom, $mapTo, $errorName $this->mapper->mapViolation($violation, $parent); + $expectedErrors = new FormErrorBag(); + $expectedErrors->addError($this->getFormError()); + if (self::LEVEL_0 === $target) { + $childErrors = new FormErrorBag(); + $childErrors->addCollection($grandChildName, new FormErrorBag()); + $expectedErrors->addCollection($childName, $childErrors); + $this->assertCount(0, $errorChild->getErrors(), $errorName.' should not have an error, but has one'); - $this->assertEquals(array($this->getFormError()), $parent->getErrors(), $parent->getName().' should have an error, but has none'); + $this->assertEquals($expectedErrors, $parent->getErrors(), $parent->getName().' should have an error, but has none'); $this->assertCount(0, $child->getErrors(), $childName.' should not have an error, but has one'); $this->assertCount(0, $grandChild->getErrors(), $grandChildName.' should not have an error, but has one'); } elseif (self::LEVEL_1 === $target) { + $expectedErrors->addCollection($grandChildName, new FormErrorBag()); + $this->assertCount(0, $errorChild->getErrors(), $errorName.' should not have an error, but has one'); $this->assertCount(0, $parent->getErrors(), $parent->getName().' should not have an error, but has one'); - $this->assertEquals(array($this->getFormError()), $child->getErrors(), $childName.' should have an error, but has none'); + $this->assertEquals($expectedErrors, $child->getErrors(), $childName.' should have an error, but has none'); $this->assertCount(0, $grandChild->getErrors(), $grandChildName.' should not have an error, but has one'); } elseif (self::LEVEL_1B === $target) { - $this->assertEquals(array($this->getFormError()), $errorChild->getErrors(), $errorName.' should have an error, but has none'); + $this->assertEquals($expectedErrors, $errorChild->getErrors(), $errorName.' should have an error, but has none'); $this->assertCount(0, $parent->getErrors(), $parent->getName().' should not have an error, but has one'); $this->assertCount(0, $child->getErrors(), $childName.' should not have an error, but has one'); $this->assertCount(0, $grandChild->getErrors(), $grandChildName.' should not have an error, but has one'); @@ -1416,7 +1451,7 @@ public function testCustomFormErrorMapping($target, $mapFrom, $mapTo, $errorName $this->assertCount(0, $errorChild->getErrors(), $errorName.' should not have an error, but has one'); $this->assertCount(0, $parent->getErrors(), $parent->getName().' should not have an error, but has one'); $this->assertCount(0, $child->getErrors(), $childName.' should not have an error, but has one'); - $this->assertEquals(array($this->getFormError()), $grandChild->getErrors(), $grandChildName.' should have an error, but has none'); + $this->assertEquals($expectedErrors, $grandChild->getErrors(), $grandChildName.' should have an error, but has none'); } } @@ -1460,18 +1495,27 @@ public function testErrorMappingForFormInheritingParentData($target, $childName, $this->mapper->mapViolation($violation, $parent); + $expectedErrors = new FormErrorBag(); + $expectedErrors->addError($this->getFormError()); + if (self::LEVEL_0 === $target) { - $this->assertEquals(array($this->getFormError()), $parent->getErrors(), $parent->getName().' should have an error, but has none'); + $childErrors = new FormErrorBag(); + $childErrors->addCollection($grandChildName, new FormErrorBag()); + $expectedErrors->addCollection($childName, $childErrors); + + $this->assertEquals($expectedErrors, $parent->getErrors(), $parent->getName().' should have an error, but has none'); $this->assertCount(0, $child->getErrors(), $childName.' should not have an error, but has one'); $this->assertCount(0, $grandChild->getErrors(), $grandChildName.' should not have an error, but has one'); } elseif (self::LEVEL_1 === $target) { + $expectedErrors->addCollection($grandChildName, new FormErrorBag()); + $this->assertCount(0, $parent->getErrors(), $parent->getName().' should not have an error, but has one'); - $this->assertEquals(array($this->getFormError()), $child->getErrors(), $childName.' should have an error, but has none'); + $this->assertEquals($expectedErrors, $child->getErrors(), $childName.' should have an error, but has none'); $this->assertCount(0, $grandChild->getErrors(), $grandChildName.' should not have an error, but has one'); } else { $this->assertCount(0, $parent->getErrors(), $parent->getName().' should not have an error, but has one'); $this->assertCount(0, $child->getErrors(), $childName.' should not have an error, but has one'); - $this->assertEquals(array($this->getFormError()), $grandChild->getErrors(), $grandChildName.' should have an error, but has none'); + $this->assertEquals($expectedErrors, $grandChild->getErrors(), $grandChildName.' should have an error, but has none'); } } } diff --git a/src/Symfony/Component/Form/Tests/FormErrorBagTest.php b/src/Symfony/Component/Form/Tests/FormErrorBagTest.php new file mode 100644 index 0000000000000..e5a27152c1484 --- /dev/null +++ b/src/Symfony/Component/Form/Tests/FormErrorBagTest.php @@ -0,0 +1,119 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Tests; + +use Symfony\Component\Form\FormError; +use Symfony\Component\Form\FormErrorBag; + +class FormErrorBagTest extends \Symfony\Component\Form\Tests\FormPerformanceTestCase +{ + public function testIterateErrors() + { + $collection = new FormErrorBag(true); + $collection->addCollection('date', new FormErrorBag()); + $collection->addCollection('checkbox', new FormErrorBag()); + $collection->addError(new FormError('This value should not be blank.')); + + $this->assertCount(1, $collection); + foreach ($collection as $error) { + $this->assertInstanceof('Symfony\Component\Form\FormError', $error); + $this->assertEquals('This value should not be blank.', $error->getMessage()); + } + } + + public function testRecursivelyIterateErrors() + { + $collection = new FormErrorBag(); + $collection->addError(new FormError('This value should not be blank.')); + + $childrenCollection = new FormErrorBag(); + $childrenCollection->addError(new FormError('This value is not a valid email.')); + $childrenCollection->addError(new FormError('This value is not a valid date.')); + $collection->addCollection('user', $childrenCollection); + + $iterator = new \RecursiveIteratorIterator($collection); + $messages = array( + '', // because we use next() in the loop + 'This value should not be blank.', + 'This value is not a valid email.', + 'This value is not a valid date.', + ); + foreach ($iterator as $error) { + $this->assertInstanceof('Symfony\Component\Form\FormError', $error); + $this->assertEquals(next($messages), $error->getMessage()); + } + } + + public function testCountingErrors() + { + $collection = new FormErrorBag(); + $collection->addError(new FormError('This value should not be blank.')); + $collection->addError(new FormError('This value should not be blank.')); + + $childrenCollection = new FormErrorBag(); + $childrenCollection->addError(new FormError('This value is not a valid email.')); + $childrenCollection->addError(new FormError('This value is not a valid date.')); + $collection->addCollection('user', $childrenCollection); + + $this->assertCount(2, $collection); + } + + public function testCoutingAllErrors() + { + $collection = new FormErrorBag(); + $collection->addError(new FormError('This value should not be blank.')); + + $childrenCollection = new FormErrorBag(); + $childrenCollection->addError(new FormError('This value is not a valid email.')); + $childrenCollection->addError(new FormError('This value is not a valid date.')); + $collection->addCollection('user', $childrenCollection); + + $this->assertEquals(3, $collection->countAll()); + } + + public function testFormNameAsKeys() + { + $collection = new FormErrorBag(); + $collection->addError(new FormError('This value should not be blank.')); + + $childrenCollection = new FormErrorBag(); + $childrenCollection->addError(new FormError('This value is not a valid email.')); + $childrenCollection->addError(new FormError('This value is not a valid date.')); + $collection->addCollection('user', $childrenCollection); + + $iterator = new \RecursiveIteratorIterator($collection); + $keys = array( + '', // use of next() in loop + '0', + 'user', + 'user', + ); + foreach ($iterator as $name => $error) { + $this->assertEquals(next($keys), $name); + } + } + + public function testToString() + { + $collection = new FormErrorBag(); + $collection->addError(new FormError('This value should not be blank.')); + + $childrenCollection = new FormErrorBag(); + $childrenCollection->addError(new FormError('This value is not a valid email.')); + $childrenCollection->addError(new FormError('This value is not a valid date.')); + $collection->addCollection('user', $childrenCollection); + + $collection->addCollection('date', new FormErrorBag()); + + $this->assertEquals("ERROR: This value should not be blank.\nuser:\n ERROR: This value is not a valid email.\n ERROR: This value is not a valid date.\ndate:\n No errors\n", (string) $collection); + } +} diff --git a/src/Symfony/Component/Form/Tests/SimpleFormTest.php b/src/Symfony/Component/Form/Tests/SimpleFormTest.php index bedad6761feef..528114257a8a9 100644 --- a/src/Symfony/Component/Form/Tests/SimpleFormTest.php +++ b/src/Symfony/Component/Form/Tests/SimpleFormTest.php @@ -664,7 +664,7 @@ public function testSubmitResetsErrors() $this->form->addError(new FormError('Error!')); $this->form->submit('foobar'); - $this->assertSame(array(), $this->form->getErrors()); + $this->assertCount(0, $this->form->getErrors()); } public function testCreateView() From b6bc8c02aaf9d036e851e6434d68cb3367dad6d3 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Sat, 28 Dec 2013 20:18:59 +0100 Subject: [PATCH 2/3] Fixed test --- src/Symfony/Component/Form/FormErrorBag.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Form/FormErrorBag.php b/src/Symfony/Component/Form/FormErrorBag.php index 235ef221c41d1..65688adbdb333 100644 --- a/src/Symfony/Component/Form/FormErrorBag.php +++ b/src/Symfony/Component/Form/FormErrorBag.php @@ -89,7 +89,7 @@ public function rewind() public function valid() { - return false === key($this->errors); + return null !== key($this->errors); } /** From f002137b9b7a0bf21cb7f2f131d1f8f589800dc9 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Sat, 28 Dec 2013 20:19:11 +0100 Subject: [PATCH 3/3] [INVALID] Tried to fix the tests --- .../Resources/views/Form/form_errors.html.php | 2 +- src/Symfony/Component/Form/FormErrorBag.php | 8 ++++++++ src/Symfony/Component/Form/Tests/FormErrorBagTest.php | 2 ++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_errors.html.php b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_errors.html.php index da9bec42ec543..0fed30228a74e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_errors.html.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_errors.html.php @@ -1,4 +1,4 @@ - +
  • getMessage() ?>
  • diff --git a/src/Symfony/Component/Form/FormErrorBag.php b/src/Symfony/Component/Form/FormErrorBag.php index 65688adbdb333..54e0f3d5214f7 100644 --- a/src/Symfony/Component/Form/FormErrorBag.php +++ b/src/Symfony/Component/Form/FormErrorBag.php @@ -89,6 +89,14 @@ public function rewind() public function valid() { + while (current($this->errors) instanceof FormErrorBag) { + $this->next(); + + if (!$this->valid()) { + return false; + } + } + return null !== key($this->errors); } diff --git a/src/Symfony/Component/Form/Tests/FormErrorBagTest.php b/src/Symfony/Component/Form/Tests/FormErrorBagTest.php index e5a27152c1484..b505819e57161 100644 --- a/src/Symfony/Component/Form/Tests/FormErrorBagTest.php +++ b/src/Symfony/Component/Form/Tests/FormErrorBagTest.php @@ -51,6 +51,8 @@ public function testRecursivelyIterateErrors() $this->assertInstanceof('Symfony\Component\Form\FormError', $error); $this->assertEquals(next($messages), $error->getMessage()); } + + $this->assertFalse(next($messages), 'got all errors'); } public function testCountingErrors()