8000 [Validator] Add support for `ConstraintViolationList::createFromMessage()` by lyrixx · Pull Request #41154 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CHANGELOG
=========

5.4
---
* Add support for `ConstraintViolationList::createFromMessage()`

5.3
---
* Add the `normalizer` option to the `Unique` constraint
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Validator/ConstraintViolationList.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public function __construct(array $violations = [])
}
}

public static function createFromMessage(string $message): self
{
$self = new self();
$self->add(new ConstraintViolation($message, '', [], null, '', null));

return $self;
}

/**
* Converts the violation into a string for debugging purposes.
*
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/Validator/Tests/ConstraintViolationListTest.php < 8BFF svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-copy">
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@ public function findByCodesProvider()
];
}

public function testCreateFromMessage()
{
$list = ConstraintViolationList::createFromMessage('my message');

$this->assertCount(1, $list);
$this->assertInstanceOf(ConstraintViolation::class, $list[0]);
$this->assertSame('my message', $list[0]->getMessage());
}

protected function getViolation($message, $root = null, $propertyPath = null, $code = null)
{
return new ConstraintViolation($message, $message, [], $root, $propertyPath, null, null, $code);
Expand Down
0