From d5c2c30a82a57e3700023c2565509887b5211ea7 Mon Sep 17 00:00:00 2001 From: ABGEO Date: Fri, 24 Apr 2020 00:32:03 +0400 Subject: [PATCH 1/5] :sparkles: #36544 Allow multiple atPath() on ConstraintViolationBuilder. --- .../Violation/ConstraintViolationBuilder.php | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php b/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php index 72de7f1a16d38..29ce45a513c80 100644 --- a/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php +++ b/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php @@ -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; @@ -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; @@ -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; } @@ -158,17 +160,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 ($this->propertyPaths 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 + )); + } } } From 8675f3accb0ed8e0feb1c26f6337b1b05f2b51ca Mon Sep 17 00:00:00 2001 From: ABGEO Date: Sat, 25 Apr 2020 13:43:43 +0400 Subject: [PATCH 2/5] :bug: #36544 [Validator] Fix single path issue. --- .../Validator/Violation/ConstraintViolationBuilder.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php b/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php index 29ce45a513c80..69e64deabb04e 100644 --- a/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php +++ b/src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php @@ -146,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, @@ -160,7 +162,7 @@ public function addViolation() ); } - foreach ($this->propertyPaths as $propertyPath) { + foreach ($paths as $propertyPath) { $this->violations->add(new ConstraintViolation( $translatedMessage, $this->message, From 98ed621ef045c1c8ffc9e48b0e4a318c37402649 Mon Sep 17 00:00:00 2001 From: ABGEO Date: Sat, 25 Apr 2020 15:21:20 +0400 Subject: [PATCH 3/5] :white_check_mark: #36544 [Validator] Cover part of ConstraintViolationBuilder with tests. --- .../Tests/ConstraintViolationBuilderTest.php | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/Symfony/Component/Validator/Tests/ConstraintViolationBuilderTest.php diff --git a/src/Symfony/Component/Validator/Tests/ConstraintViolationBuilderTest.php b/src/Symfony/Component/Validator/Tests/ConstraintViolationBuilderTest.php new file mode 100644 index 0000000000000..9522b0d679bda --- /dev/null +++ b/src/Symfony/Component/Validator/Tests/ConstraintViolationBuilderTest.php @@ -0,0 +1,57 @@ + + * + * 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()); + } +} From 452a57fc50ff231522877057d4f2d4f6350752b7 Mon Sep 17 00:00:00 2001 From: ABGEO Date: Sat, 25 Apr 2020 15:25:02 +0400 Subject: [PATCH 4/5] :pencil: #36544 [Validator] Update CHANGELOG. --- src/Symfony/Component/Validator/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Symfony/Component/Validator/CHANGELOG.md b/src/Symfony/Component/Validator/CHANGELOG.md index d2eb00fc42c5a..26f7685a276f6 100644 --- a/src/Symfony/Component/Validator/CHANGELOG.md +++ b/src/Symfony/Component/Validator/CHANGELOG.md @@ -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 From 6281443c20026e7dc5930846a5a80b80a76e1d35 Mon Sep 17 00:00:00 2001 From: ABGEO Date: Sat, 25 Apr 2020 22:06:57 +0400 Subject: [PATCH 5/5] :rotating_light: #36544 [Validator] Fix linter warnings. --- .../Validator/Tests/ConstraintViolationBuilderTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Validator/Tests/ConstraintViolationBuilderTest.php b/src/Symfony/Component/Validator/Tests/ConstraintViolationBuilderTest.php index 9522b0d679bda..9e98022560782 100644 --- a/src/Symfony/Component/Validator/Tests/ConstraintViolationBuilderTest.php +++ b/src/Symfony/Component/Validator/Tests/ConstraintViolationBuilderTest.php @@ -29,7 +29,7 @@ protected function setUp(): void $this->builder = new ConstraintViolationBuilder( $this->violations, new NotBlank(), - "myMessage", + 'myMessage', [], null, 'root', @@ -38,7 +38,8 @@ protected function setUp(): void ); } - public function testMultipleAtPathCall() { + public function testMultipleAtPathCall() + { $this->builder ->atPath('firstName') ->atPath('lastName')