Skip to content

Commit 785ca88

Browse files
committed
[Validator][DX] Add isEmpty() method to ConstraintViolationListInterface
1 parent 9a5003e commit 785ca88

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ CHANGELOG
2020
`maxPropertyPath` options
2121
* added a new `notInRangeMessage` option to the `Range` constraint that will
2222
be used in the violation builder when both `min` and `max` are not null
23+
* Added `isEmpty()` method to `ConstraintViolationListInterface`.
2324

2425
4.3.0
2526
-----

src/Symfony/Component/Validator/ConstraintViolationList.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ public function remove($offset)
105105
unset($this->violations[$offset]);
106106
}
107107

108+
/**
109+
* {@inheritdoc}
110+
*/
111+
public function isEmpty(): bool
112+
{
113+
return empty($this->violations);
114+
}
115+
108116
/**
109117
* {@inheritdoc}
110118
*

src/Symfony/Component/Validator/ConstraintViolationListInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* A list of constraint violations.
1616
*
1717
* @author Bernhard Schussek <bschussek@gmail.com>
18+
*
19+
* @method bool isEmpty() Is the list empty? - not implementing it is deprecated since Symfony 4.4
1820
*/
1921
interface ConstraintViolationListInterface extends \Traversable, \Countable, \ArrayAccess
2022
{

src/Symfony/Component/Validator/Tests/ConstraintViolationListTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,19 @@ public function findByCodesProvider()
155155
];
156156
}
157157

158+
public function testIsEmpty()
159+
{
160+
$list = new ConstraintViolationList();
161+
162+
$this->assertTrue($list->isEmpty());
163+
164+
$list->set(0, $this->getViolation('Error'));
165+
$this->assertFalse($list->isEmpty());
166+
167+
$list->remove(0);
168+
$this->assertTrue($list->isEmpty());
169+
}
170+
158171
protected function getViolation($message, $root = null, $propertyPath = null, $code = null)
159172
{
160173
return new ConstraintViolation($message, $message, [], $root, $propertyPath, null, null, $code);

0 commit comments

Comments
 (0)