8000 merged branch Tobion/formhaserrors (PR #5356) · sunqipenglib/symfony@867e31c · GitHub
[go: up one dir, main page]

Skip to content

Commit 867e31c

Browse files
committed
merged branch Tobion/formhaserrors (PR symfony#5356)
Commits ------- d5eb4f7 [Form] fix phpdoc of Form::hasErrors 5cb8264 [Form] deprecated Form::hasErrors that isn't part of the Interface Discussion ---------- [Form] deprecated Form::hasErrors that isn't part of the Interface This method is not part of FormInterface, so I deprecated it as it cannot be used reliably. This is consistent with other hassers that were deprecated like `hasChildren` where one should use `count` instead. --------------------------------------------------------------------------- by stof at 2012-08-26T19:11:19Z You should deprecate it, not remove it --------------------------------------------------------------------------- by Tobion at 2012-08-26T19:17:35Z oh right. I thought it was added in 2.1 and thus can be removed but it's also in 2.0. Done. --------------------------------------------------------------------------- by bschussek at 2012-08-29T11:00:32Z :+1:
2 parents 003f8c5 + d5eb4f7 commit 867e31c

File tree

4 files changed

+54
-54
lines changed

4 files changed

+54
-54
lines changed

src/Symfony/Component/Form/Form.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ public function isValid()
708708
throw new \LogicException('You cannot call isValid() on a form that is not bound.');
709709
}
710710

711-
if ($this->hasErrors()) {
711+
if (count($this->errors) > 0) {
712712
return false;
713713
}
714714

@@ -724,15 +724,15 @@ public function isValid()
724724
}
725725

726726
/**
727-
* Returns whether or not there are errors.
727+
* Returns whether there are errors associated with this form.
728728
*
729-
* @return Boolean true if form is bound and not valid
729+
* @return Boolean
730+
*
731+
* @deprecated Deprecated since version 2.1, to be removed in 2.3. Count
732+
* {@link getErrors()} instead.
730733
*/
731734
public function hasErrors()
732735
{
733-
// Don't call isValid() here, as its semantics are slightly different
734-
// Forms are not valid if their children are invalid, but
735-
// hasErrors() returns only true if a form itself has errors
736736
return count($this->errors) > 0;
737737
}
738738

src/Symfony/Component/Form/FormInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public function getPropertyPath();
184184
public function addError(FormError $error);
185185

186186
/**
187-
* Returns whether the form is valid.
187+
* Returns whether the form and all children are valid.
188188
*
189189
* @return Boolean
190190
*/

src/Symfony/Component/Form/Tests/Extension/Validator/ViolationMapper/ViolationMapperTest.php

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ public function testMapToVirtualFormIfDataDoesNotMatch()
124124

125125
$this->mapper->mapViolation($violation, $parent);
126126

127-
$this->assertFalse($parent->hasErrors(), $parent->getName() . ' should not have an error, but has one');
127+
$this->assertCount(0, $parent->getErrors(), $parent->getName() . ' should not have an error, but has one');
128128
$this->assertEquals(array($this->getFormError()), $child->getErrors(), $child->getName() . ' should have an error, but has none');
129-
$this->assertFalse($grandChild->hasErrors(), $grandChild->getName() . ' should not have an error, but has one');
129+
$this->assertCount(0, $grandChild->getErrors(), $grandChild->getName() . ' should not have an error, but has one');
130130
}
131131

132132
public function testFollowDotRules()
@@ -149,9 +149,9 @@ public function testFollowDotRules()
149149

150150
$this->mapper->mapViolation($violation, $parent);
151151

152-
$this->assertFalse($parent->hasErrors(), $parent->getName() . ' should not have an error, but has one');
153-
$this->assertFalse($child->hasErrors(), $child->getName() . ' should not have an error, but has one');
154-
$this->assertFalse($grandChild->hasErrors(), $grandChild->getName() . ' should not have an error, but has one');
152+
$this->assertCount(0, $parent->getErrors(), $parent->getName() . ' should not have an error, but has one');
153+
$this->assertCount(0, $child->getErrors(), $child->getName() . ' should not have an error, but has one');
154+
$this->assertCount(0, $grandChild->getErr F438 ors(), $grandChild->getName() . ' should not have an error, but has one');
155155
$this->assertEquals(array($this->getFormError()), $grandGrandChild->getErrors(), $grandGrandChild->getName() . ' should have an error, but has none');
156156
}
157157

@@ -172,9 +172,9 @@ public function testAbortMappingIfNotSynchronized()
172172

173173
$this->mapper->mapViolation($violation, $parent);
174174

175-
$this->assertFalse($parent->hasErrors(), $parent->getName() . ' should not have an error, but has one');
176-
$this->assertFalse($child->hasErrors(), $child->getName() . ' should not have an error, but has one');
177-
$this->assertFalse($grandChild->hasErrors(), $grandChild->getName() . ' should not have an error, but has one');
175+
$this->assertCount(0, $parent->getErrors(), $parent->getName() . ' should not have an error, but has one');
176+
$this->assertCount(0, $child->getErrors(), $child->getName() . ' should not have an error, but has one');
177+
$this->assertCount(0, $grandChild->getErrors(), $grandChild->getName() . ' should not have an error, but has one');
178178
}
179179

180180
public function testAbortVirtualFormMappingIfNotSynchronized()
@@ -194,9 +194,9 @@ public function testAbortVirtualFormMappingIfNotSynchronized()
194194

195195
$this->mapper->mapViolation($violation, $parent);
196196

197-
$this->assertFalse($parent->hasErrors(), $parent->getName() . ' should not have an error, but has one');
198-
$this->assertFalse($child->hasErrors(), $child->getName() . ' should not have an error, but has one');
199-
$this->assertFalse($grandChild->hasErrors(), $grandChild->getName() . ' should not have an error, but has one');
197+
$this->assertCount(0, $parent->getErrors(), $parent->getName() . ' should not have an error, but has one');
198+
$this->assertCount(0, $child->getErrors(), $child->getName() . ' should not have an error, but has one');
199+
$this->assertCount(0, $grandChild->getErrors(), $grandChild->getName() . ' should not have an error, but has one');
200200
}
201201

202202
public function testAbortDotRuleMappingIfNotSynchronized()
@@ -218,9 +218,9 @@ public function testAbortDotRuleMappingIfNotSynchronized()
218218

219219
$this->mapper->mapViolation($violation, $parent);
220220

221-
$this->assertFalse($parent->hasErrors(), $parent->getName() . ' should not have an error, but has one');
222-
$this->assertFalse($child->hasErrors(), $child->getName() . ' should not have an error, but has one');
223-
$this->assertFalse($grandChild->hasErrors(), $grandChild->getName() . ' should not have an error, but has one');
221+
$this->assertCount(0, $parent->getErrors(), $parent->getName() . ' should not have an error, but has one');
222+
$this->assertCount(0, $child->getErrors(), $child->getName() . ' should not have an error, but has one');
223+
$this->assertCount(0, $grandChild->getErrors(), $grandChild->getName() . ' should not have an error, but has one');
224224
}
225225

226226
public function provideDefaultTests()
@@ -732,15 +732,15 @@ public function testDefaultErrorMapping($target, $childName, $childPath, $grandC
732732

733733
if (self::LEVEL_0 === $target) {
734734
$this->assertEquals(array($this->getFormError()), $parent->getErrors(), $parent->getName() . ' should have an error, but has none');
735-
$this->assertFalse($child->hasErrors(), $childName . ' should not have an error, but has one');
736-
$this->assertFalse($grandChild->hasErrors(), $grandChildName . ' should not have an error, but has one');
735+
$this->assertCount(0, $child->getErrors(), $childName . ' should not have an error, but has one');
736+
$this->assertCount(0, $grandChild->getErrors(), $grandChildName . ' should not have an error, but has one');
737737
} elseif (self::LEVEL_1 === $target) {
738-
$this->assertFalse($parent->hasErrors(), $parent->getName() . ' should not have an error, but has one');
738+
$this->assertCount(0, $parent->getErrors(), $parent->getName() . ' should not have an error, but has one');
739739
$this->assertEquals(array($this->getFormError()), $child->getErrors(), $childName . ' should have an error, but has none');
740-
$this->assertFalse($grandChild->hasErrors(), $grandChildName . ' should not have an error, but has one');
740+
$this->assertCount(0, $grandChild->getErrors(), $grandChildName . ' should not have an error, but has one');
741741
} else {
742-
$this->assertFalse($parent->hasErrors(), $parent->getName() . ' should not have an error, but has one');
743-
$this->assertFalse($child->hasErrors(), $childName . ' should not have an error, but has one');
742+
$this->assertCount(0, $parent->getErrors(), $parent->getName() . ' should not have an error, but has one');
743+
$this->assertCount(0, $child->getErrors(), $childName . ' should not have an error, but has one');
744744
$this->assertEquals(array($this->getFormError()), $grandChild->getErrors(), $grandChildName. ' should have an error, but has none');
745745
}
746746
}
@@ -1199,20 +1199,20 @@ public function testCustomDataErrorMapping($target, $mapFrom, $mapTo, $childName
11991199
$this->mapper->mapViolation($violation, $parent);
12001200

12011201
if ($target !== self::LEVEL_0) {
1202-
$this->assertFalse($distraction->hasErrors(), 'distraction should not have an error, but has one');
1202+
$this->assertCount(0, $distraction->getErrors(), 'distraction should not have an error, but has one');
12031203
}
12041204

12051205
if (self::LEVEL_0 === $target) {
12061206
$this->assertEquals(array($this->getFormError()), $parent->getErrors(), $parent->getName() . ' should have an error, but has none');
1207-
$this->assertFalse($child->hasErrors(), $childName . ' should not have an error, but has one');
1208-
$this->assertFalse($grandChild->hasErrors(), $grandChildName . ' should not have an error, but has one');
1207+
$this->assertCount(0, $child->getErrors(), $childName . ' should not have an error, but has one');
1208+
$this->assertCount(0, $grandChild->getErrors(), $grandChildName . ' should not have an error, but has one');
12091209
} elseif (self::LEVEL_1 === $target) {
1210-
$this->assertFalse($parent->hasErrors(), $parent->getName() . ' should not have an error, but has one');
1210+
$this->assertCount(0, $parent->getErrors(), $parent->getName() . ' should not have an error, but has one');
12111211
$this->assertEquals(array($this->getFormError()), $child->getErrors(), $childName . ' should have an error, but has none');
1212-
$this->assertFalse($grandChild->hasErrors(), $grandChildName . ' should not have an error, but has one');
1212+
$this->assertCount(0, $grandChild->getErrors(), $grandChildName . ' should not have an error, but has one');
12131213
} else {
1214-
$this->assertFalse($parent->hasErrors(), $parent->getName() . ' should not have an error, but has one');
1215-
$this->assertFalse($child->hasErrors(), $childName . ' should not have an error, but has one');
1214+
$this->assertCount(0, $parent->getErrors(), $parent->getName() . ' should not have an error, but has one');
1215+
$this->assertCount(0, $child->getErrors(), $childName . ' should not have an error, but has one');
12161216
$this->assertEquals(array($this->getFormError()), $grandChild->getErrors(), $grandChildName. ' should have an error, but has none');
12171217
}
12181218
}
@@ -1385,24 +1385,24 @@ public function testCustomFormErrorMapping($target, $mapFrom, $mapTo, $errorName
13851385
$this->mapper->mapViolation($violation, $parent);
13861386

13871387
if (self::LEVEL_0 === $target) {
1388-
$this->assertFalse($errorChild->hasErrors(), $errorName . ' should not have an error, but has one');
1388+
$this->assertCount(0, $errorChild->getErrors(), $errorName . ' should not have an error, but has one');
13891389
$this->assertEquals(array($this->getFormError()), $parent->getErrors(), $parent->getName() . ' should have an error, but has none');
1390-
$this->assertFalse($child->hasErrors(), $childName . ' should not have an error, but has one');
1391-
$this->assertFalse($grandChild->hasErrors(), $grandChildName . ' should not have an error, but has one');
1390+
$this->assertCount(0, $child->getErrors(), $childName . ' should not have an error, but has one');
1391+
$this->assertCount(0, $grandChild->getErrors(), $grandChildName . ' should not have an error, but has one');
13921392
} elseif (self::LEVEL_1 === $target) {
1393-
$this->assertFalse($errorChild->hasErrors(), $errorName . ' should not have an error, but has one');
1394-
$this->assertFalse($parent->hasErrors(), $parent->getName() . ' should not have an error, but has one');
1393+
$this->assertCount(0, $errorChild->getErrors(), $errorName . ' should not have an error, but has one');
1394+
$this->assertCount(0, $parent->getErrors(), $parent->getName() . ' should not have an error, but has one');
13951395
$this->assertEquals(array($this->getFormError()), $child->getErrors(), $childName . ' should have an error, but has none');
1396-
$this->assertFalse($grandChild->hasErrors(), $grandChildName . ' should not have an error, but has one');
1396+
$this->assertCount(0, $grandChild->getErrors(), $grandChildName . ' should not have an error, but has one');
13971397
} elseif (self::LEVEL_1B === $target) {
13981398
$this->assertEquals(array($this->getFormError()), $errorChild->getErrors(), $errorName . ' should have an error, but has none');
1399-
$this->assertFalse($parent->hasErrors(), $parent->getName() . ' should not have an error, but has one');
1400-
$this->assertFalse($child->hasErrors(), $childName . ' should not have an error, bu F438 t has one');
1401-
$this->assertFalse($grandChild->hasErrors(), $grandChildName . ' should not have an error, but has one');
1399+
$this->assertCount(0, $parent->getErrors(), $parent->getName() . ' should not have an error, but has one');
1400+
$this->assertCount(0, $child->getErrors(), $childName . ' should not have an error, but has one');
1401+
$this->assertCount(0, $grandChild->getErrors(), $grandChildName . ' should not have an error, but has one');
14021402
} else {
1403-
$this->assertFalse($errorChild->hasErrors(), $errorName . ' should not have an error, but has one');
1404-
$this->assertFalse($parent->hasErrors(), $parent->getName() . ' should not have an error, but has one');
1405-
$this->assertFalse($child->hasErrors(), $childName . ' should not have an error, but has one');
1403+
$this->assertCount(0, $errorChild->getErrors(), $errorName . ' should not have an error, but has one');
1404+
$this->assertCount(0, $parent->getErrors(), $parent->getName() . ' should not have an error, but has one');
1405+
$this->assertCount(0, $child->getErrors(), $childName . ' should not have an error, but has one');
14061406
$this->assertEquals(array($this->getFormError()), $grandChild->getErrors(), $grandChildName. ' should have an error, but has none');
14071407
}
14081408
}
@@ -1449,15 +1449,15 @@ public function testVirtualFormErrorMapping($target, $childName, $childPath, $gr
14491449

14501450
if (self::LEVEL_0 === $target) {
14511451
$this->assertEquals(array($this->getFormError()), $parent->getErrors(), $parent->getName() . ' should have an error, but has none');
1452-
$this->assertFalse($child->hasErrors(), $childName . ' should not have an error, but has one');
1453-
$this->assertFalse($grandChild->hasErrors(), $grandChildName . ' should not have an error, but has one');
1452+
$this->assertCount(0, $child->getErrors(), $childName . ' should not have an error, but has one');
1453+
$this->assertCount(0, $grandChild->getErrors(), $grandChildName . ' should not have an error, but has one');
14541454
} elseif (self::LEVEL_1 === $target) {
1455-
$this->assertFalse($parent->hasErrors(), $parent->getName() . ' should not have an error, but has one');
1455+
$this->assertCount(0, $parent->getErrors(), $parent->getName() . ' should not have an error, but has one');
14561456
$this->assertEquals(array($this->getFormError()), $child->getErrors(), $childName . ' should have an error, but has none');
1457-
$this->assertFalse($grandChild->hasErrors(), $grandChildName . ' should not have an error, but has one');
1457+
$this->assertCount(0, $grandChild->getErrors(), $grandChildName . ' should not have an error, but has one');
14581458
} else {
1459-
$this->assertFalse($parent->hasErrors(), $parent->getName() . ' should not have an error, but has one');
1460-
$this->assertFalse($child->hasErrors(), $childName . ' should not have an error, but has one');
1459+
$this->assertCount(0, $parent->getErrors(), $parent->getName() . ' should not have an error, but has one');
1460+
$this->assertCount(0, $child->getErrors(), $childName . ' should not have an error, but has one');
14611461
$this->assertEquals(array($this->getFormError()), $grandChild->getErrors(), $grandChildName. ' should have an error, but has none');
14621462
}
14631463
}

src/Symfony/Component/Form/Tests/SimpleFormTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,12 @@ public function testHasErrors()
231231
{
232232
$this->form->addError(new FormError('Error!'));
233233

234-
$this->assertTrue($this->form->hasErrors());
234+
$this->assertCount(1, $this->form->getErrors());
235235
}
236236

237237
public function testHasNoErrors()
238238
{
239-
$this->assertFalse($this->form->hasErrors());
239+
$this->assertCount(0, $this->form->getErrors());
240240
}
241241

242242
/**

0 commit comments

Comments
 (0)
0