Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc2d4be1

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 parents5b07e0a +daac66e commitc2d4be1

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
publicfunctiongetErrors($deep =false,$flatten =true)
188188
{
189-
$errors =array();
190-
191-
returnnewFormErrorIterator($errors,$this,$deep,$flatten);
189+
returnnewFormErrorIterator($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
publicfunctiongetErrors($deep =false,$flatten =true)
782782
{
783-
returnnewFormErrorIterator($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 ($thisas$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 ($iteratoras$error) {
801+
$errors[] =$error;
802+
}
803+
}else {
804+
$errors[] =$iterator;
805+
}
806+
}
807+
}
808+
809+
returnnewFormErrorIterator($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
namespaceSymfony\Component\Form;
1313

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

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

4647
/**
47-
* @varBoolean
48+
* @varFormError[]|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-
publicfunction__construct(array &$errors,FormInterface$form,$deep =false,$flatten =true)
60+
publicfunction__construct(FormInterface$form,array$errors)
7261
{
73-
$this->errors = &$errors;
74-
$this->form =$form;
75-
$this->deep =$deep;
76-
$this->flatten =$flatten;
62+
foreach ($errorsas$error) {
63+
if (!($errorinstanceof FormError ||$errorinstanceof self)) {
64+
thrownewInvalidArgumentException(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->elementsas$element) {
91-
if ($elementinstanceof FormError) {
92-
$string .='ERROR:'.$element->getMessage()."\n";
86+
foreach ($this->errorsas$error) {
87+
if ($errorinstanceof 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 iteratorfor nested
117-
* errors.
112+
* @return FormError|FormErrorIterator An error or an iteratorcontaining
113+
*nestederrors.
118114
*/
119115
publicfunctioncurrent()
120116
{
121-
returncurrent($this->elements);
117+
returncurrent($this->errors);
122118
}
123119

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

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

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

152148
/**
@@ -157,32 +153,7 @@ public function valid()
157153
*/
158154
publicfunctionrewind()
159155
{
160-
$this->elements =$this->errors;
161-
162-
if ($this->deep) {
163-
foreach ($this->formas$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 ($iteratoras$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
publicfunctionoffsetExists($position)
196167
{
197-
returnisset($this->elements[$position]);
168+
returnisset($this->errors[$position]);
198169
}
199170

200171
/**
@@ -208,11 +179,11 @@ public function offsetExists($position)
208179
*/
209180
publicfunctionoffsetGet($position)
210181
{
211-
if (!isset($this->elements[$position])) {
182+
if (!isset($this->errors[$position])) {
212183
thrownewOutOfBoundsException('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
publicfunctionhasChildren()
245216
{
246-
returncurrent($this->elements)instanceof self;
217+
returncurrent($this->errors)instanceof self;
247218
}
248219

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

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

279250
/**
@@ -285,14 +256,14 @@ public function count()
285256
*/
286257
publicfunctionseek($position)
287258
{
288-
if (!isset($this->elements[$position])) {
259+
if (!isset($this->errors[$position])) {
289260
thrownewOutOfBoundsException('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)

[8]ページ先頭

©2009-2025 Movatter.jp