1111
1212namespace Symfony \Component \Form ;
1313
14+ use Symfony \Component \Form \Exception \InvalidArgumentException ;
1415use Symfony \Component \Form \Exception \OutOfBoundsException ;
1516use 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