Skip to content

Commit c2d4be1

Browse files
committed
feature #10418 [Form] Removed "magic" from FormErrorIterator (webmozart)
This PR was merged into the 2.5-dev branch. Discussion ---------- [Form] Removed "magic" from FormErrorIterator | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- daac66e [Form] Removed "magic" from FormErrorIterator
2 parents 5b07e0a + daac66e commit c2d4be1

File tree

3 files changed

+71
-76
lines changed

3 files changed

+71
-76
lines changed

src/Symfony/Component/Form/Button.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,7 @@ public function all()
186186
*/
187187
public function getErrors($deep = false, $flatten = true)
188188
{
189-
$errors = array();
190-
191-
return new FormErrorIterator($errors, $this, $deep, $flatten);
189+
return new FormErrorIterator($this, array());
192190
}
193191

194192
/**

src/Symfony/Component/Form/Form.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,33 @@ public function getClickedButton()
780780
*/
781781
public function getErrors($deep = false, $flatten = true)
782782
{
783-
return new FormErrorIterator($this->errors, $this, $deep, $flatten);
783+
$errors = $this->errors;
784+
785+
// Copy the errors of nested forms to the $errors array
786+
if ($deep) {
787+
foreach ($this as $child) {
788+
/** @var FormInterface $child */
789+
if ($child->isSubmitted() && $child->isValid()) {
790+
continue;
791+
}
792+
793+
$iterator = $child->getErrors(true, $flatten);
794+
795+
if (0 === count($iterator)) {
796+
continue;
797+
}
798+
799+
if ($flatten) {
800+
foreach ($iterator as $error) {
801+
$errors[] = $error;
802+
}
803+
} else {
804+
$errors[] = $iterator;
805+
}
806+
}
807+
}
808+
809+
return new FormErrorIterator($this, $errors);
784810
}
785811

786812
/**

src/Symfony/Component/Form/FormErrorIterator.php

Lines changed: 43 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Form;
1313

14+
use Symfony\Component\Form\Exception\InvalidArgumentException;
1415
use Symfony\Component\Form\Exception\OutOfBoundsException;
1516
use Symfony\Component\Form\Exception\BadMethodCallException;
1617

@@ -44,38 +45,33 @@ class FormErrorIterator implements \RecursiveIterator, \SeekableIterator, \Array
4445
private $form;
4546

4647
/**
47-
* @var Boolean
48+
* @var FormError[]|FormErrorIterator[]
4849
*/
49-
private $deep;
50-
51-
/**
52-
* @var Boolean
53-
*/
54-
private $flatten;
55-
56-
/**
57-
* @var array
58-
*/
59-
private $elements;
50+
private $errors;
6051

6152
/**
6253
* Creates a new iterator.
6354
*
64-
* @param array $errors The iterated errors
65-
* @param FormInterface $form The form the errors belong to
66-
* @param Boolean $deep Whether to include the errors of child
67-
* forms
68-
* @param Boolean $flatten Whether to flatten the recursive list of
69-
* errors into a flat list
55+
* @param FormInterface $form The erroneous form
56+
* @param array $errors The form errors
57+
*
58+
* @throws InvalidArgumentException If the errors are invalid
7059
*/
71-
public function __construct(array &$errors, FormInterface $form, $deep = false, $flatten = true)
60+
public function __construct(FormInterface $form, array $errors)
7261
{
73-
$this->errors = &$errors;
74-
$this->form = $form;
75-
$this->deep = $deep;
76-
$this->flatten = $flatten;
62+
foreach ($errors as $error) {
63+
if (!($error instanceof FormError || $error instanceof self)) {
64+
throw new InvalidArgumentException(sprintf(
65+
'The errors must be instances of '.
66+
'"\Symfony\Component\Form\FormError" or "%s". Got: "%s".',
67+
__CLASS__,
68+
is_object($error) ? get_class($error) : gettype($error)
69+
));
70+
}
71+
}
7772

78-
$this->rewind();
73+
$this->form = $form;
74+
$this->errors = $errors;
7975
}
8076

8177
/**
@@ -87,13 +83,13 @@ public function __toString()
8783
{
8884
$string = '';
8985

90-
foreach ($this->elements as $element) {
91-
if ($element instanceof FormError) {
92-
$string .= 'ERROR: '.$element->getMessage()."\n";
86+
foreach ($this->errors as $error) {
87+
if ($error instanceof FormError) {
88+
$string .= 'ERROR: '.$error->getMessage()."\n";
9389
} else {
94-
/** @var $element FormErrorIterator */
95-
$string .= $element->form->getName().":\n";
96-
$string .= self::indent((string) $element);
90+
/** @var $error FormErrorIterator */
91+
$string .= $error->form->getName().":\n";
92+
$string .= self::indent((string) $error);
9793
}
9894
}
9995

@@ -113,20 +109,20 @@ public function getForm()
113109
/**
114110
* Returns the current element of the iterator.
115111
*
116-
* @return FormError|FormErrorIterator An error or an iterator for nested
117-
* errors.
112+
* @return FormError|FormErrorIterator An error or an iterator containing
113+
* nested errors.
118114
*/
119115
public function current()
120116
{
121-
return current($this->elements);
117+
return current($this->errors);
122118
}
123119

124120
/**
125121
* Advances the iterator to the next position.
126122
*/
127123
public function next()
128124
{
129-
next($this->elements);
125+
next($this->errors);
130126
}
131127

132128
/**
@@ -136,7 +132,7 @@ public function next()
136132
*/
137133
public function key()
138134
{
139-
return key($this->elements);
135+
return key($this->errors);
140136
}
141137

142138
/**
@@ -146,7 +142,7 @@ public function key()
146142
*/
147143
public function valid()
148144
{
149-
return null !== key($this->elements);
145+
return null !== key($this->errors);
150146
}
151147

152148
/**
@@ -157,32 +153,7 @@ public function valid()
157153
*/
158154
public function rewind()
159155
{
160-
$this->elements = $this->errors;
161-
162-
if ($this->deep) {
163-
foreach ($this->form as $child) {
164-
/** @var FormInterface $child */
165-
if ($child->isSubmitted() && $child->isValid()) {
166-
continue;
167-
}
168-
169-
$iterator = $child->getErrors(true, $this->flatten);
170-
171-
if (0 === count($iterator)) {
172-
continue;
173-
}
174-
175-
if ($this->flatten) {
176-
foreach ($iterator as $error) {
177-
$this->elements[] = $error;
178-
}
179-
} else {
180-
$this->elements[] = $iterator;
181-
}
182-
}
183-
}
184-
185-
reset($this->elements);
156+
reset($this->errors);
186157
}
187158

188159
/**
@@ -194,7 +165,7 @@ public function rewind()
194165
*/
195166
public function offsetExists($position)
196167
{
197-
return isset($this->elements[$position]);
168+
return isset($this->errors[$position]);
198169
}
199170

200171
/**
@@ -208,11 +179,11 @@ public function offsetExists($position)
208179
*/
209180
public function offsetGet($position)
210181
{
211-
if (!isset($this->elements[$position])) {
182+
if (!isset($this->errors[$position])) {
212183
throw new OutOfBoundsException('The offset '.$position.' does not exist.');
213184
}
214185

215-
return $this->elements[$position];
186+
return $this->errors[$position];
216187
}
217188

218189
/**
@@ -243,15 +214,15 @@ public function offsetUnset($position)
243214
*/
244215
public function hasChildren()
245216
{
246-
return current($this->elements) instanceof self;
217+
return current($this->errors) instanceof self;
247218
}
248219

249220
/**
250221
* Alias of {@link current()}.
251222
*/
252223
public function getChildren()
253224
{
254-
return current($this->elements);
225+
return current($this->errors);
255226
}
256227

257228
/**
@@ -273,7 +244,7 @@ public function getChildren()
273244
*/
274245
public function count()
275246
{
276-
return count($this->elements);
247+
return count($this->errors);
277248
}
278249

279250
/**
@@ -285,14 +256,14 @@ public function count()
285256
*/
286257
public function seek($position)
287258
{
288-
if (!isset($this->elements[$position])) {
259+
if (!isset($this->errors[$position])) {
289260
throw new OutOfBoundsException('The offset '.$position.' does not exist.');
290261
}
291262

292-
reset($this->elements);
263+
reset($this->errors);
293264

294-
while ($position !== key($this->elements)) {
295-
next($this->elements);
265+
while ($position !== key($this->errors)) {
266+
next($this->errors);
296267
}
297268
}
298269

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