Skip to content

Commit e9fceb9

Browse files
committed
Add Result::getPropertyErrorMessagesByPath() method
1 parent 2c97207 commit e9fceb9

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 2.4.1 under development
44

5-
- no changes in this release.
5+
- New #774: Add `Result::getPropertyErrorMessagesByPath()` method (@vjik)
66

77
## 2.4.0 July 07, 2025
88

docs/guide/en/result.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ The output for example above:
8686
];
8787
```
8888

89+
#### Filtering by a specific path
90+
91+
This list of error messages can be filtered by a specific path to property.
92+
93+
```php
94+
$result->getPropertyErrorMessagesByPath(['person', 'first_name']);
95+
```
96+
8997
### Error messages indexed by property
9098

9199
To group error messages by property, use the following `Result` API call:

src/Result.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Stringable;
99

1010
use function array_slice;
11+
use function count;
1112
use function implode;
1213
use function is_string;
1314

@@ -202,6 +203,23 @@ public function getPropertyErrorMessages(string $property): array
202203
return $errors;
203204
}
204205

206+
/**
207+
* Get an array of error messages for the path specified.
208+
*
209+
* @psalm-param list<string> $path
210+
* @psalm-return list<string>
211+
*/
212+
public function getPropertyErrorMessagesByPath(array $path): array
213+
{
214+
$errors = [];
215+
foreach ($this->errors as $error) {
216+
if ($path === array_slice($error->getValuePath(), 0, count($path))) {
217+
$errors[] = $error->getMessage();
218+
}
219+
}
220+
return $errors;
221+
}
222+
205223
/**
206224
* Get arrays of error messages for the property specified indexed by property path.
207225
* Each key is a dot-separated property path.

tests/ResultTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,23 @@ public function testGetAttributeErrorMessages(): void
189189
$this->assertEquals(['error4.1', 'error4.2'], $result->getPropertyErrorMessages('attribute4'));
190190
}
191191

192+
public function testGetPropertyErrorMessagesByPath(): void
193+
{
194+
$result = (new Result())
195+
->addError('e1', valuePath: ['age'])
196+
->addError('e2', valuePath: ['person'])
197+
->addError('e3', valuePath: ['person', 'first_name'])
198+
->addError('e4', valuePath: ['person', 'first_name'])
199+
->addError('e5', valuePath: ['person', 'last_name'])
200+
->addError('e6');
201+
202+
$this->assertSame(['e1', 'e2', 'e3', 'e4', 'e5', 'e6'], $result->getPropertyErrorMessagesByPath([]));
203+
$this->assertSame([], $result->getPropertyErrorMessagesByPath(['non-exists']));
204+
$this->assertSame(['e1'], $result->getPropertyErrorMessagesByPath(['age']));
205+
$this->assertSame(['e2', 'e3', 'e4', 'e5'], $result->getPropertyErrorMessagesByPath(['person']));
206+
$this->assertSame(['e3', 'e4'], $result->getPropertyErrorMessagesByPath(['person', 'first_name']));
207+
}
208+
192209
public function testGetAttributeErrorMessagesIndexedByPath(): void
193210
{
194211
$result = $this->createAttributeErrorResult();

0 commit comments

Comments
 (0)