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

Appearance settings

[Validator] Allow multiple atPath() on ConstraintViolationBuilder #36553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
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
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
5.1.0
-----

* allow multiple `atPath()` on ConstraintViolationBuilder
* added the `Hostname` constraint and validator
* added the `alpha3` option to the `Country` and `Language` constraints
* allow to define a reusable set of constraints by extending the `Compound` constraint
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\Violation\ConstraintViolationBuilder;

class ConstraintViolationBuilderTest extends TestCase
{
private $violations;
private $builder;

protected function setUp(): void
{
$this->violations = new ConstraintViolationList();

$this->builder = new ConstraintViolationBuilder(
$this->violations,
new NotBlank(),
'myMessage',
[],
null,
'root',
null,
new Translator('en_EN')
);
}

public function testMultipleAtPathCall()
{
$this->builder
->atPath('firstName')
->atPath('lastName')
->atPath('email')
->addViolation();

$violationFirst = $this->violations->get(0);
$violationLast = $this->violations->get(2);

$this->assertCount(3, $this->violations);
$this->assertEquals('root.firstName', $violationFirst->getPropertyPath());
$this->assertEquals('myMessage', $violationFirst->getMessage());
$this->assertEquals('root.email', $violationLast->getPropertyPath());
$this->assertEquals('myMessage', $violationLast->getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface
private $parameters;
private $root;
private $invalidValue;
private $propertyPath;
private $basePropertyPath;
private $propertyPaths;
private $translator;
private $translationDomain;
private $plural;
Expand All @@ -52,7 +53,8 @@ public function __construct(ConstraintViolationList $violations, Constraint $con
$this->message = $message;
$this->parameters = $parameters;
$this->root = $root;
$this->propertyPath = $propertyPath;
$this->basePropertyPath = $propertyPath;
$this->propertyPaths = [];
$this->invalidValue = $invalidValue;
$this->translator = $translator;
$this->translationDomain = $translationDomain;
Expand All @@ -64,7 +66,7 @@ public function __construct(ConstraintViolationList $violations, Constraint $con
*/
public function atPath(string $path)
{
$this->propertyPath = PropertyPath::append($this->propertyPath, $path);
$this->propertyPaths[] = PropertyPath::append($this->basePropertyPath, $path);

return $this;
}
Expand Down Expand Up @@ -144,6 +146,8 @@ public function setCause($cause)
*/
public function addViolation()
{
$paths = empty($this->propertyPaths) ? [$this->basePropertyPath] : $this->propertyPaths;

if (null === $this->plural) {
$translatedMessage = $this->translator->trans(
$this->message,
Expand All @@ -158,17 +162,19 @@ public function addViolation()
);
}

$this->violations->add(new ConstraintViolation(
$translatedMessage,
$this->message,
$this->parameters,
$this->root,
$this->propertyPath,
$this->invalidValue,
$this->plural,
$this->code,
$this->constraint,
$this->cause
));
foreach ($paths as $propertyPath) {
$this->violations->add(new ConstraintViolation(
$translatedMessage,
$this->message,
$this->parameters,
$this->root,
$propertyPath,
$this->invalidValue,
$this->plural,
$this->code,
$this->constraint,
$this->cause
));
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.