11
11
12
12
namespace Symfony \Component \Form ;
13
13
14
+ use Symfony \Component \Form \Exception \InvalidArgumentException ;
14
15
use Symfony \Component \Form \Exception \OutOfBoundsException ;
15
16
use Symfony \Component \Form \Exception \BadMethodCallException ;
16
17
@@ -44,38 +45,33 @@ class FormErrorIterator implements \RecursiveIterator, \SeekableIterator, \Array
44
45
private $ form ;
45
46
46
47
/**
47
- * @var Boolean
48
+ * @var FormError[]|FormErrorIterator[]
48
49
*/
49
- private $ deep ;
50
-
51
- /**
52
- * @var Boolean
53
- */
54
- private $ flatten ;
55
-
56
- /**
57
- * @var array
58
- */
59
- private $ elements ;
50
+ private $ errors ;
60
51
61
52
/**
62
53
* Creates a new iterator.
63
54
*
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
70
59
*/
71
- public function __construct (array & $ errors , FormInterface $ form , $ deep = false , $ flatten = true )
60
+ public function __construct (FormInterface $ form , array $ errors )
72
61
{
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
+ }
77
72
78
- $ this ->rewind ();
73
+ $ this ->form = $ form ;
74
+ $ this ->errors = $ errors ;
79
75
}
80
76
81
77
/**
@@ -87,13 +83,13 @@ public function __toString()
87
83
{
88
84
$ string = '' ;
89
85
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" ;
93
89
} 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 );
97
93
}
98
94
}
99
95
@@ -113,20 +109,20 @@ public function getForm()
113
109
/**
114
110
* Returns the current element of the iterator.
115
111
*
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.
118
114
*/
119
115
public function current ()
120
116
{
121
- return current ($ this ->elements );
117
+ return current ($ this ->errors );
122
118
}
123
119
124
120
/**
125
121
* Advances the iterator to the next position.
126
122
*/
127
123
public function next ()
128
124
{
129
- next ($ this ->elements );
125
+ next ($ this ->errors );
130
126
}
131
127
132
128
/**
@@ -136,7 +132,7 @@ public function next()
136
132
*/
137
133
public function key ()
138
134
{
139
- return key ($ this ->elements );
135
+ return key ($ this ->errors );
140
136
}
141
137
142
138
/**
@@ -146,7 +142,7 @@ public function key()
146
142
*/
147
143
public function valid ()
148
144
{
149
- return null !== key ($ this ->elements );
145
+ return null !== key ($ this ->errors );
150
146
}
151
147
152
148
/**
@@ -157,32 +153,7 @@ public function valid()
157
153
*/
158
154
public function rewind ()
159
155
{
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 );
186
157
}
187
158
188
159
/**
@@ -194,7 +165,7 @@ public function rewind()
194
165
*/
195
166
public function offsetExists ($ position )
196
167
{
197
- return isset ($ this ->elements [$ position ]);
168
+ return isset ($ this ->errors [$ position ]);
198
169
}
199
170
200
171
/**
@@ -208,11 +179,11 @@ public function offsetExists($position)
208
179
*/
209
180
public function offsetGet ($ position )
210
181
{
211
- if (!isset ($ this ->elements [$ position ])) {
182
+ if (!isset ($ this ->errors [$ position ])) {
212
183
throw new OutOfBoundsException ('The offset ' .$ position .' does not exist. ' );
213
184
}
214
185
215
- return $ this ->elements [$ position ];
186
+ return $ this ->errors [$ position ];
216
187
}
217
188
218
189
/**
@@ -243,15 +214,15 @@ public function offsetUnset($position)
243
214
*/
244
215
public function hasChildren ()
245
216
{
246
- return current ($ this ->elements ) instanceof self;
217
+ return current ($ this ->errors ) instanceof self;
247
218
}
248
219
249
220
/**
250
221
* Alias of {@link current()}.
251
222
*/
252
223
public function getChildren ()
253
224
{
254
- return current ($ this ->elements );
225
+ return current ($ this ->errors );
255
226
}
256
227
257
228
/**
@@ -273,7 +244,7 @@ public function getChildren()
273
244
*/
274
245
public function count ()
275
246
{
276
- return count ($ this ->elements );
247
+ return count ($ this ->errors );
277
248
}
278
249
279
250
/**
@@ -285,14 +256,14 @@ public function count()
285
256
*/
286
257
public function seek ($ position )
287
258
{
288
- if (!isset ($ this ->elements [$ position ])) {
259
+ if (!isset ($ this ->errors [$ position ])) {
289
260
throw new OutOfBoundsException ('The offset ' .$ position .' does not exist. ' );
290
261
}
291
262
292
- reset ($ this ->elements );
263
+ reset ($ this ->errors );
293
264
294
- while ($ position !== key ($ this ->elements )) {
295
- next ($ this ->elements );
265
+ while ($ position !== key ($ this ->errors )) {
266
+ next ($ this ->errors );
296
267
}
297
268
}
298
269
0 commit comments