10000 [Form] Changed Form::getErrors() to return an iterator and added two optional parameters $deep and $flatten by webmozart · Pull Request #9918 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Changed Form::getErrors() to return an iterator and added two optional parameters $deep and $flatten #9918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 11, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
[Form] Changed the default value of $flatten in Form::getErrors() to …
…true
  • Loading branch information
webmozart committed Jan 10, 2014
commit 6b3fbb59052cf5bf5fca789bc39fae5f74d99a3e
2 changes: 1 addition & 1 deletion UPGRADE-2.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ Form
After:

```
public function getErrors($deep = false, $flatten = false)
public function getErrors($deep = false, $flatten = true)
{
```
6 changes: 3 additions & 3 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -193,7 +193,7 @@ UPGRADE FROM 2.x to 3.0
After:

```
echo $form->getErrors(true);
echo $form->getErrors(true, false);
```


Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public function all()
/**
* {@inheritdoc}
*/
public function getErrors($deep = false, $flatten = false)
public function getErrors($deep = false, $flatten = true)
{
$errors = array();

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ public function getClickedButton()
/**
* {@inheritdoc}
*/
public function getErrors($deep = false, $flatten = false)
public function getErrors($deep = false, $flatten = true)
{
return new FormErrorIterator($this->errors, $this, $deep, $flatten);
}
Expand All @@ -797,7 +797,7 @@ public function getErrors($deep = false, $flatten = false)
*/
public function getErrorsAsString($level = 0)
{
return self::indent((string) $this->getErrors(true), $level);
return self::indent((string) $this->getErrors(true, false), $level);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/FormErrorIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be $this->elements rather than $this->errors ? Or shouldn't you at least declare the $errors property ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I don't really see the point of having a duplicate between $this->elements and $this->errors, or to have a reference from the $errors array

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@webmozart this->errors is not declared

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. See #10418

685C

$this->form = $form;
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Form/FormInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function all();
* @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.
Expand Down
38 changes: 19 additions & 19 deletions src/Symfony/Component/Form/Tests/CompoundFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -875,24 +875,17 @@ public function testGetErrorsDeep()
$this->assertSame(
"ERROR: Error 1\n".
"ERROR: Error 2\n".
"Child:\n".
" ERROR: Nested Error\n",
"ERROR: Nested Error\n",
(string) $errors
);

$errorsAsArray = iterator_to_array($errors);

$this->assertSame($error1, $errorsAsArray[0]);
$this->assertSame($error2, $errorsAsArray[1]);
$this->assertInstanceOf('Symfony\Component\Form\FormErrorIterator', $errorsAsArray[2]);

$nestedErrorsAsArray = iterator_to_array($errorsAsArray[2]);

$this->assertCount(1, $nestedErrorsAsArray);
$this->assertSame($nestedError, $nestedErrorsAsArray[0]);
$this->assertSame(
array($error1, $error2, $nestedError),
iterator_to_array($errors)
);
}

public function testGetErrorsDeepFlat()
public function testGetErrorsDeepRecursive()
{
$this->form->addError($error1 = new FormError('Error 1'));
$this->form->addError($error2 = new FormError('Error 2'));
Expand All @@ -901,19 +894,26 @@ public function testGetErrorsDeepFlat()
$childForm->addError($nestedError = new FormError('Nested Error'));
$this->form->add($childForm);

$errors = $this->form->getErrors(true, true);
$errors = $this->form->getErrors(true, false);

$this->assertSame(
"ERROR: Error 1\n".
"ERROR: Error 2\n".
"ERROR: Nested Error\n",
"Child:\n".
" ERROR: Nested Error\n",
(string) $errors
);

$this->assertSame(
array($error1, $error2, $nestedError),
iterator_to_array($errors)
);
$errorsAsArray = iterator_to_array($errors);

$this->assertSame($error1, $errorsAsArray[0]);
$this->assertSame($error2, $errorsAsArray[1]);
$this->assertInstanceOf('Symfony\Component\Form\FormErrorIterator', $errorsAsArray[2]);

$nestedErrorsAsArray = iterator_to_array($errorsAsArray[2]);

$this->assertCount(1, $nestedErrorsAsArray);
$this->assertSame($nestedError, $nestedErrorsAsArray[0]);
}

// Basic cases are covered in SimpleFormTest
Expand Down
0