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 all commits
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
39 changes: 39 additions & 0 deletions UPGRADE-2.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,42 @@ Routing
-------

* Added a new optional parameter `$requiredSchemes` to `Symfony\Component\Routing\Generator\UrlGenerator::doGenerate()`

Form
----

* The method `FormInterface::getErrors()` now returns an instance of
`Symfony\Component\Form\FormErrorIterator` instead of an array. This object
is traversable, countable and supports array access. However, you can not
pass it to any of PHP's `array_*` functions anymore. You should use
`iterator_to_array()` in those cases where you did.

Before:

```
$errors = array_map($callback, $form->getErrors());
```

After:

```
$errors = array_map($callback, iterator_to_array($form->getErrors()));
```

* The method `FormInterface::getErrors()` now has two additional, optional
parameters. Make sure to add these parameters to the method signatures of
your implementations of that interface.

Before:

```
public function getErrors()
{
```

After:

```
public function getErrors($deep = false, $flatten = true)
{
```
16 changes: 16 additions & 0 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,22 @@ UPGRADE FROM 2.x to 3.0
* The options "csrf_provider" and "intention" were renamed to "csrf_token_generator"
and "csrf_token_id".

* The method `Form::getErrorsAsString()` was removed. Use `Form::getErrors()`
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:

```
echo $form->getErrorsAsString();
```

After:

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


### FrameworkBundle

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php if ($errors): ?>
<?php if (count($errors) > 0): ?>
<ul>
<?php foreach ($errors as $error): ?>
<li><?php echo $error->getMessage() ?></li>
Expand Down
6 changes: 4 additions & 2 deletions src/Symfony/Component/Form/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,11 @@ public function all()
/**
* {@inheritdoc}
*/
public function getErrors()
public function getErrors($deep = false, $flatten = true)
{
return array();
$errors = array();

return new FormErrorIterator($errors, $this, $deep, $flatten);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ CHANGELOG
* added an option for multiple files upload
* form errors now reference their cause (constraint violation, exception, ...)
* form errors now remember which form they were originally added to
* [BC BREAK] added two optional parameters to FormInterface::getErrors() and
changed the method to return a Symfony\Component\Form\FormErrorIterator
instance instead of an array

2.4.0
-----
Expand Down
38 changes: 21 additions & 17 deletions src/Symfony/Component/Form/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -778,9 +778,9 @@ public function getClickedButton()
/**
* {@inheritdoc}
*/
public function getErrors()
public function getErrors($deep = false, $flatten = true)
{
return $this->errors;
return new FormErrorIterator($this->errors, $this, $deep, $flatten);
}

/**
Expand All @@ -791,24 +791,13 @@ public function getErrors()
* @param integer $level The indentation level (used internally)
*
* @return string A string representation of all errors
*
* @deprecated Deprecated since version 2.5, to be removed in 3.0. Use
* {@link getErrors()} instead and cast the result to a string.
*/
public function getErrorsAsString($level = 0)
{
$errors = '';
foreach ($this->errors as $error) {
$errors .= str_repeat(' ', $level).'ERROR: '.$error->getMessage()."\n";
}

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";
}
}

return $errors;
return self::indent((string) $this->getErrors(true, false), $level);
}

/**
Expand Down Expand Up @@ -1115,4 +1104,19 @@ private function viewToNorm($value)

return $value;
}

/**
* Utility function for indenting multi-line strings.
*
* @param string $string The string
* @param integer $level The number of spaces to use for indentation
*
* @return string The indented string
*/
private static function indent($string, $level)
{
$indentation = str_repeat(' ', $level);

return rtrim($indentation.str_replace("\n", "\n".$indentation, $string), ' ');
}
}
Loading