Skip to content

Commit 01a0758

Browse files
committed
[Validator] Add compared value path to violation parameters
1 parent 64ed22b commit 01a0758

7 files changed

+56
-3
lines changed

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

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

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
*/

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
}

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
}

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
}

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)