Skip to content

Commit ce81199

Browse files
committed
feature #9918 [Form] Changed Form::getErrors() to return an iterator and added two optional parameters $deep and $flatten (webmozart)
This PR was merged into the 2.5-dev branch. Discussion ---------- [Form] Changed Form::getErrors() to return an iterator and added two optional parameters $deep and $flatten | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | yes | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #7205 | License | MIT | Doc PR | - See the changes in the UPGRADE files for more information. Commits ------- 6b3fbb5 [Form] Changed the default value of $flatten in Form::getErrors() to true a9268c4 [Form] Changed Form::getErrors() to return an iterator and added two optional parameters $deep and $flatten
2 parents f15ea50 + 6b3fbb5 commit ce81199

File tree

11 files changed

+516
-40
lines changed

11 files changed

+516
-40
lines changed

UPGRADE-2.5.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,42 @@ Routing
55
-------
66

77
* Added a new optional parameter `$requiredSchemes` to `Symfony\Component\Routing\Generator\UrlGenerator::doGenerate()`
8+
9+
Form
10+
----
11+
12+
* The method `FormInterface::getErrors()` now returns an instance of
13+
`Symfony\Component\Form\FormErrorIterator` instead of an array. This object
14+
is traversable, countable and supports array access. However, you can not
15+
pass it to any of PHP's `array_*` functions anymore. You should use
16+
`iterator_to_array()` in those cases where you did.
17+
18+
Before:
19+
20+
```
21+
$errors = array_map($callback, $form->getErrors());
22+
```
23+
24+
After:
25+
26+
```
27+
$errors = array_map($callback, iterator_to_array($form->getErrors()));
28+
```
29+
30+
* The method `FormInterface::getErrors()` now has two additional, optional
31+
parameters. Make sure to add these parameters to the method signatures of
32+
your implementations of that interface.
33+
34+
Before:
35+
36+
```
37+
public function getErrors()
38+
{
39+
```
40+
41+
After:
42+
43+
```
44+
public function getErrors($deep = false, $flatten = true)
45+
{
46+
```

UPGRADE-3.0.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,22 @@ UPGRADE FROM 2.x to 3.0
246246
* The options "csrf_provider" and "intention" were renamed to "csrf_token_generator"
247247
and "csrf_token_id".
248248

249+
* The method `Form::getErrorsAsString()` was removed. Use `Form::getErrors()`
250+
instead with the argument `$deep` set to true and `$flatten` set to false
251+
and cast the returned iterator to a string (if not done implicitly by PHP).
252+
253+
Before:
254+
255+
```
256+
echo $form->getErrorsAsString();
257+
```
258+
259+
After:
260+
261+
```
262+
echo $form->getErrors(true, false);
263+
```
264+
249265

250266
### FrameworkBundle
251267

src/Symfony/Bundle/FrameworkBundle/Resources/views/Form/form_errors.html.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php if ($errors): ?>
1+
<?php if (count($errors) > 0): ?>
22
<ul>
33
<?php foreach ($errors as $error): ?>
44
<li><?php echo $error->getMessage() ?></li>

src/Symfony/Component/Form/Button.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,11 @@ public function all()
184184
/**
185185
* {@inheritdoc}
186186
*/
187-
public function getErrors()
187+
public function getErrors($deep = false, $flatten = true)
188188
{
189-
return array();
189+
$errors = array();
190+
191+
return new FormErrorIterator($errors, $this, $deep, $flatten);
190192
}
191193

192194
/**

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ CHANGELOG
77
* added an option for multiple files upload
88
* form errors now reference their cause (constraint violation, exception, ...)
99
* form errors now remember which form they were originally added to
10+
* [BC BREAK] added two optional parameters to FormInterface::getErrors() and
11+
changed the method to return a Symfony\Component\Form\FormErrorIterator
12+
instance instead of an array
1013

1114
2.4.0
1215
-----

src/Symfony/Component/Form/Form.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -778,9 +778,9 @@ public function getClickedButton()
778778
/**
779779
* {@inheritdoc}
780780
*/
781-
public function getErrors()
781+
public function getErrors($deep = false, $flatten = true)
782782
{
783-
return $this->errors;
783+
return new FormErrorIterator($this->errors, $this, $deep, $flatten);
784784
}
785785

786786
/**
@@ -791,24 +791,13 @@ public function getErrors()
791791
* @param integer $level The indentation level (used internally)
792792
*
793793
* @return string A string representation of all errors
794+
*
795+
* @deprecated Deprecated since version 2.5, to be removed in 3.0. Use
796+
* {@link getErrors()} instead and cast the result to a string.
794797
*/
795798
public function getErrorsAsString($level = 0)
796799
{
797-
$errors = '';
798-
foreach ($this->errors as $error) {
799-
$errors .= str_repeat(' ', $level).'ERROR: '.$error->getMessage()."\n";
800-
}
801-
802-
foreach ($this->children as $key => $child) {
803-
$errors .= str_repeat(' ', $level).$key.":\n";
804-
if ($child instanceof self && $err = $child->getErrorsAsString($level + 4)) {
805-
$errors .= $err;
806-
} else {
807-
$errors .= str_repeat(' ', $level + 4)."No errors\n";
808-
}
809-
}
810-
811-
return $errors;
800+
return self::indent((string) $this->getErrors(true, false), $level);
812801
}
813802

814803
/**
@@ -1115,4 +1104,19 @@ private function viewToNorm($value)
11151104

11161105
return $value;
11171106
}
1107+
1108+
/**
1109+
* Utility function for indenting multi-line strings.
1110+
*
1111+
* @param string $string The string
1112+
* @param integer $level The number of spaces to use for indentation
1113+
*
1114+
* @return string The indented string
1115+
*/
1116+
private static function indent($string, $level)
1117+
{
1118+
$indentation = str_repeat(' ', $level);
1119+
1120+
return rtrim($indentation.str_replace("\n", "\n".$indentation, $string), ' ');
1121+
}
11181122
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy