8000 feature #31526 [Validator] Add compared value path to violation param… · symfony/symfony@129bf73 · GitHub
[go: up one dir, main page]

Skip to content

Commit 129bf73

Browse files
committed
feature #31526 [Validator] Add compared value path to violation parameters (ogizanagi)
This PR was merged into the 4.4 branch. Discussion ---------- [Validator] Add compared value path to violation parameters | Q | A | ------------- | --- | Branch? | 4.4 <!-- see below --> | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | N/A <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | N/A While it's not really useful to use as a placeholder in the violation message template (compared to hard-coding it into the message. Nor it is really user-friendly), it becomes handy in conjunction with #29130 for any mapping logic on client-side. Commits ------- 2da226a [Validator] Add compared value path to violation parameters
2 parents 6f9d0f0 + 2da226a commit 129bf73

7 files changed

+56
-3
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
4.4.0
5+
-----
6+
7+
* added the `compared_value_path` parameter in violations when using any
8+
comparison constraint with the `propertyPath` option.
9+
410
4.3.0
511
-----
612

src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,17 @@ public function validate($value, Constraint $constraint)
7777
}
7878

7979
if (!$this->compareValues($value, $comparedValue)) {
80-
$this->context->buildViolation($constraint->message)
80+
$violationBuilder = $this->context->buildViolation($constraint->message)
8181
->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING | self::PRETTY_DATE))
8282
->setParameter('{{ compared_value }}', $this->formatValue($comparedValue, self::OBJECT_TO_STRING | self::PRETTY_DATE))
8383
->setParameter('{{ compared_value_type }}', $this->formatTypeOf($comparedValue))
84-
->setCode($this->getErrorCode())
85-
->addViolation();
84+
->setCode($this->getErrorCode());
85+
86+
if (null !== $path) {
87+
$violationBuilder->setParameter('{{ compared_value_path }}', $path);
88+
}
89+
90+
$violationBuilder->addViolation();
8691
}
8792
}
8893

src/Symfony/Component/Validator/Tests/Constraints/AbstractComparisonValidatorTestCase.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,28 @@ public function testInvalidComparisonToValue($dirtyValue, $dirtyValueAsString, $
231231
->assertRaised();
232232
}
233233

234+
public function testInvalidComparisonToPropertyPathAddsPathAsParameter()
235+
{
236+
list($dirtyValue, $dirtyValueAsString, $comparedValue, $comparedValueString, $comparedValueType) = current($this->provideAllInvalidComparisons());
237+
238+
$constraint = $this->createConstraint(['propertyPath' => 'value']);
239+
$constraint->message = 'Constraint Message';
240+
241+
$object = new ComparisonTest_Class($comparedValue);
242+
243+
$this->setObject($object);
244+
245+
$this->validator->validate($dirtyValue, $constraint);
246+
247+
$this->buildViolation('Constraint Message')
248+
->setParameter('{{ value }}', $dirtyValueAsString)
249+
->setParameter('{{ compared_value }}', $comparedValueString)
250+
->setParameter('{{ compared_value_path }}', 'value')
251+
->setParameter('{{ compared_value_type }}', $comparedValueType)
252+
->setCode($this->getErrorCode())
253+
->assertRaised();
254+
}
255+
234256
/**
235257
* @return array
236258
*/

src/Symfony/Component/Validator/Tests/Constraints/GreaterThanOrEqualValidatorWithPositiveOrZeroConstraintTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,9 @@ public function testValidComparisonToPropertyPathOnArray($comparedValue)
108108
{
109109
$this->markTestSkipped('PropertyPath option is not used in Positive constraint');
110110
}
111+
112+
public function testInvalidComparisonToPropertyPathAddsPathAsParameter()
113+
{
114+
$this->markTestSkipped('PropertyPath option is not used in PositiveOrZero constraint');
115+
}
111116
}

src/Symfony/Component/Validator/Tests/Constraints/GreaterThanValidatorWithPositiveConstraintTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,9 @@ public function testValidComparisonToPropertyPathOnArray($comparedValue)
111111
{
112112
$this->markTestSkipped('PropertyPath option is not used in Positive constraint');
113113
}
114+
115+
public function testInvalidComparisonToPropertyPathAddsPathAsParameter()
116+
{
117+
$this->markTestSkipped('PropertyPath option is not used in Positive constraint');
118+
}
114119
}

src/Symfony/Component/Validator/Tests/Constraints/LessThanOrEqualValidatorWithNegativeOrZeroConstraintTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,9 @@ public function testValidComparisonToPropertyPathOnArray($comparedValue)
111111
{
112112
$this->markTestSkipped('PropertyPath option is not used in NegativeOrZero constraint');
113113
}
114+
115+
public function testInvalidComparisonToPropertyPathAddsPathAsParameter()
116+
{
117+
$this->markTestSkipped('PropertyPath option is not used in NegativeOrZero constraint');
118+
}
114119
}

src/Symfony/Component/Validator/Tests/Constraints/LessThanValidatorWithNegativeConstraintTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,9 @@ public function testValidComparisonToPropertyPathOnArray($comparedValue)
111111
{
112112
$this->markTestSkipped('PropertyPath option is not used in Positive constraint');
113113
}
114+
115+
public function testInvalidComparisonToPropertyPathAddsPathAsParameter()
116+
{
117+
$this->markTestSkipped('PropertyPath option is not used in Negative constraint');
118+
}
114119
}

0 commit comments

Comments
 (0)
0