8000 [Form] Changed Form::getErrors() to return an iterator and added two … · symfony/symfony@a9268c4 · GitHub
[go: up one dir, main page]

Skip to content

Commit a9268c4

Browse files
committed
[Form] Changed Form::getErrors() to return an iterator and added two optional parameters $deep and $flatten
1 parent 8ea3a43 commit a9268c4

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 = false)
45+
{
46+
```

UPGRADE-3.0.md

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

183+
* The method `Form::getErrorsAsString()` was removed. Use `Form::getErrors()`
184+
instead with the argument `$deep` set to true and cast the returned iterator
185+
to a string (if not done implicitly by PHP).
186+
187+
Before:
188+
189+
```
190+
echo $form->getErrorsAsString();
191+
```
192+
193+
After:
194+
195+
```
196+
echo $form->getErrors(true);
197+
```
198+
183199

184200
### FrameworkBundle
185201

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 = false)
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 = false)
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), $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)
0